Rails 4 Postgresql array data type: updating values

I'm just starting to use the array data type in Postgres with Rails 4, but I'm having trouble getting the values ​​in the existing array for updating. I have a column called "quantity", which is an array of integers ([0,0]). I want to update the value in 'quantity [0]', and I 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 value of the number of objects in the object changes, but when I try to save the object using "a.save", this change is not reflected when the object is reloaded.

Any help would be greatly appreciated.

thanks

+4
2

http://apidock.com/rails/ActiveRecord/Dirty , ..._will_change!, . , changes .

: , ..._will_change, .

+2

<attribute>_will_change! , . , []=, .

+1

All Articles