【问题标题】:Mongoid not saving hash propertyMongoid 不保存哈希属性
【发布时间】:2016-09-02 12:06:44
【问题描述】:

我正在使用 Rails 4 和 Mongoid 4。在我的应用程序中,我有一个模型,我有一个哈希属性。我正在尝试像这样更新此属性:

user = User.find(id)
user['hash_attr']['another_attr'] = another_hash
user.save

但是上面的代码似乎并没有被持久化在数据库中。如果我像这样打印对象:

puts user['hash_attr']['another_attr']

我得到了正确的结果,所以没有错误,但我尝试再次加载模型(更新后):

user = User.find(id)
puts user['hash_attr']['another_attr']

属性未更新...我看过 this 文章,但 1) 已经很老了,所以这方面可能有一些新东西,2) 它对我不起作用。

有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails mongoid mongoid4


    【解决方案1】:

    应该是 user.hash_attr['another_attr'] 用户保存

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多