Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am just starting to use the array data-type in Postgres with Rails 4 but I am having trouble getting values on an existing array to update. I have a column called 'quantity' that is an array of integers( [0,0] ). I want to update the value at 'quantity[0]' and I have tried the following in the console:

a = Asset.find(2) 
=> #<Asset id: 2, quantity: [0,0]>

a.quantity[0] = 5
=> 5 

a.quantity_will_change! 
=> [5, 0] 

a.save
=> true

a.reload
=> #<Asset id: 2, quantity: [0,0]>

As you can see, the asset object's quantity value is change but when I try to save the object using 'a.save' the change is not reflected when I reload the object.

Any help would be greatly appreciated.

Thanks

share

1 Answer

My reading of http://apidock.com/rails/ActiveRecord/Dirty is that you have to call ..._will_change! before you change the attribute.

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.