轨道4 PostgreSQL的数组数据类型:更新值 [英] Rails 4 Postgresql array data-type: updating values

查看:648
本文介绍了轨道4 PostgreSQL的数组数据类型:更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用数组数据类型的Postgres中使用Rails 4,但我有麻烦现有阵列以更新得到的值。我有一个名为量列,它是一个整数数组([0,0])。我想更新为'量[0]'的价值,我曾尝试在控制台以下内容:

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]>

正如你可以看到,资产对象的数量值的变化,但是当我尝试使用a.save的变化不会反映保存对象时,我重新加载的对象。

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.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

感谢

推荐答案

我的阅读为http:// apidock .COM /轨/的ActiveRecord /脏是,你必须调用 ..._ will_change!前的更改属性的。您应该能够通过检查的变化证实了这一点下的各种情况。

My reading of http://apidock.com/rails/ActiveRecord/Dirty is that you have to call ..._will_change! before you change the attribute. You should be able to confirm this by examining changes under various scenarios.

更新:我刚刚测试了字符串属性的行为,它仍然保存,即使变化之前 ..._ will_change 发更新后的字符串被调用,所以我间pretation被关闭。

Update: I just tested the behavior with a string attribute, and it still saves the updated string even if the change was made before ..._will_change is called, so my interpretation may be off.

这篇关于轨道4 PostgreSQL的数组数据类型:更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆