为什么 mongoid 不在 new() 上添加我的嵌套属性?

发布于 2024-12-12 09:30:58 字数 1810 浏览 0 评论 0原文

我似乎无法弄清楚为什么当我创建新的父对象时 Mongoid 不会为子对象设置嵌套属性。我想创建一个新的 Folio,添加一个子功能,然后将其推送到配置文件上的 Folio 数组上。

我有一个配置文件,其中嵌入了许多 Folio,其中嵌入了许多功能:

class Profile
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  #regular fields here; removed for brevity

  embeds_many :folios, class_name: "Folio"
end

class Folio
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  accepts_nested_attributes_for :features
  embedded_in :profile

  field :name
  field :desc
  field :order, type: Integer, default:0
  embeds_many :features

  attr_accessible :name, :desc, :order  
end

class Feature
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  embedded_in :folio
  belongs_to :project

  field :content_type, type: Integer  #ContentType
  field :content_id
  field :txt, type: String
  field :order, type: Integer, default:0

  attr_accessible :project_id, :content_type, :content_id, :txt, :order
end

控制器:

  def new
    @folio = Folio.new
    @folio.features.build
  end

  def create
    @folio = Folio.new(params[:folio])

    #@folio.features is still empty here.

    @profile.folios << @folio
    @profile.save
    render "create_or_update.js"
  end

在创建中,参数哈希看起来不错:

{"folio"=>{"id"=>"new", "name"=>"new name", "desc"=>"new description", "features_attributes"=>{"0"=>{"project_id"=>"4ea0b68e291ebb44a100000a", "content_type"=>"1", "content_id"=>"4ea0b68e291ebb44a100000d", "txt"=>"note here"}}}, "commit"=>"Save", "action"=>"create", "controller"=>"folios"}

但 @folio.features 仍然是空的。

如果我记得的话,这对于 AR 来说效果很好。奇怪的是,Folio 上没有 features_attributes=() 方法。我认为这是嵌套属性工作所必需的?我缺少什么?

这是在 Rails 3.1 和 Mongoid 2.2.3 上。

I can't seem to figure out why Mongoid won't set the nested attributes for a child object when I create a new parent. I want to create a new Folio, add one child Feature, then push it on the Folios array on Profile.

I have a Profile, which embed many Folios, which embed many Features:

class Profile
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  #regular fields here; removed for brevity

  embeds_many :folios, class_name: "Folio"
end

class Folio
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  accepts_nested_attributes_for :features
  embedded_in :profile

  field :name
  field :desc
  field :order, type: Integer, default:0
  embeds_many :features

  attr_accessible :name, :desc, :order  
end

class Feature
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  embedded_in :folio
  belongs_to :project

  field :content_type, type: Integer  #ContentType
  field :content_id
  field :txt, type: String
  field :order, type: Integer, default:0

  attr_accessible :project_id, :content_type, :content_id, :txt, :order
end

Controller:

  def new
    @folio = Folio.new
    @folio.features.build
  end

  def create
    @folio = Folio.new(params[:folio])

    #@folio.features is still empty here.

    @profile.folios << @folio
    @profile.save
    render "create_or_update.js"
  end

In create, the param hash looks good:

{"folio"=>{"id"=>"new", "name"=>"new name", "desc"=>"new description", "features_attributes"=>{"0"=>{"project_id"=>"4ea0b68e291ebb44a100000a", "content_type"=>"1", "content_id"=>"4ea0b68e291ebb44a100000d", "txt"=>"note here"}}}, "commit"=>"Save", "action"=>"create", "controller"=>"folios"}

But @folio.features is still empty.

This worked fine with AR, if I remember. Strangely, there is no features_attributes=() method on Folio. I thought that was required for the nested attributes to work? What am I missing?

This is on Rails 3.1 with Mongoid 2.2.3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2024-12-19 09:30:59

您是否尝试过为 Folio 文档中的功能启用自动保存 true

class Folio
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  accepts_nested_attributes_for :features , :autosave => true
  embedded_in :profile
end

have you tried enabling AutoSave true for features in Folio document

class Folio
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  accepts_nested_attributes_for :features , :autosave => true
  embedded_in :profile
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文