jam*_*mes 18 ruby-on-rails rails-postgresql hstore
我遇到一个奇怪的问题,我必须是用户错误,但无法弄明白.
我正在使用Ruby 1.9.3-p194,Rails 4.01,PostgreSQL.
我有一个模型Customer,其中有一个名为data的列,它是一个hstore类型.出于某种原因,我无法使用任何新的键/值更新数据(hstore)列,也无法更新现有键的值.我可以插入并指定任何没有任何问题的键/值.
客户ID:1,first_name:"Mark",last_name:"Test",data:{"balance"=>"0"},created_at:"2013-11-27 14:39:09",updated_at:"2013- 11-27 14:39:09"
c.data["balance"] = "100"
c.save
Run Code Online (Sandbox Code Playgroud)
(0.2ms)BEGIN
(0.3ms)COMMIT => true
如果我执行update_attributes,它会保存它.
c.update_attributes({:data => {"balance" => "343"}})
Run Code Online (Sandbox Code Playgroud)
当我使用c.save时,我没有看到任何错误或异常!.有人有主意吗?
wds*_*pkr 23
我现在仍然需要这个工作,不能等待bug解决,所以这是我的解决方法:
c.data["balance"] = "100"
c.data_will_change!
c.save
Run Code Online (Sandbox Code Playgroud)
...它将保存到DB!
"attribute_name_will_change!" 方法不是很好记录,可以在Active Model Dirty模块的介绍中找到:http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
好的,终于找到了我的问题的答案.原来这是Rails 4.0中的一个错误.
"ActiveRecord Hstore bug:无法更新哈希中的密钥" https://github.com/rails/rails/issues/6127
升级后的Hstore文档:
http://edgeguides.rubyonrails.org/active_record_postgresql.html#hstore
他们正在努力为Hstore和Json对象更新使用相同的解决方案.
这将在Rails 4.2上修复.
请求:https://github.com/rails/rails/pull/15674
| 归档时间: |
|
| 查看次数: |
4793 次 |
| 最近记录: |