【问题标题】:mongoid: update nested attributesmongoid:更新嵌套属性
【发布时间】:2011-06-10 03:20:16
【问题描述】:

我遇到了一个更新嵌套属性的问题,引用文档没有被持久化

> u = User.first
 => #<User...
> u.changes
 => {} 
> u.profile.changes
 => {} 
> u.attributes = {:profile_attributes => {:weight => 8}}
 => {:profile_attributes=>{:weight=>8}} 
> u.changes
 => {} 
> u.profile.changes
 => {"weight"=>[14.0, 8.0]} 
> u.update
 => true 
> u.shipping_profile.changes
 => {"weight"=>[14.0, 8.0]}

update/update_attributes/save all 不会持久化嵌套的更改。 我在用户模型中声明了attr_accessible :profile_attributes

当父文档已经被持久化时,嵌套属性需要什么来持久化?

【问题讨论】:

    标签: ruby-on-rails mongoid persistence


    【解决方案1】:

    您的User 课程中有以下内容吗?

    accepts_nested_attributes_for :profile
    

    如果您缺少该行,我很确定它将忽略所有使用 update_attributes 通过父级更新嵌套记录的尝试。

    【讨论】:

      【解决方案2】:

      原来它是一个 mongoid 问题。我在https://github.com/mongoid/mongoid/pull/978 为遇到相同问题的其他人提交了一个测试失败的拉取请求。我只是手动保存/更新嵌套记录,直到解决

      【讨论】:

        猜你喜欢
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多