Ruby on rails 为什么';是否在new()上添加我的嵌套属性?

Ruby on rails 为什么';是否在new()上添加我的嵌套属性?,ruby-on-rails,mongoid,nested-attributes,Ruby On Rails,Mongoid,Nested Attributes,我似乎不明白为什么在创建新父对象时Mongoid不会为子对象设置嵌套属性。我想创建一个新的对开本,添加一个子功能,然后将其推送到配置文件上的对开本阵列上 我有一个配置文件,其中嵌入了许多对开本,其中嵌入了许多功能: class Profile include Mongoid::Document include Mongoid::Timestamps::Updated #regular fields here; removed for brevity embeds_many :

我似乎不明白为什么在创建新父对象时Mongoid不会为子对象设置嵌套属性。我想创建一个新的对开本,添加一个子功能,然后将其推送到配置文件上的对开本阵列上

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

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.features仍然是空的

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


这是在带有Mongoid 2.2.3的Rails 3.1上实现的。

您是否尝试过为对开本文档中的功能启用自动保存功能

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

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

工作。根据文档,嵌入式文档不需要这样做。我还必须将attr_accessible:features_属性添加到对开本中才能使其正常工作。您成功地做到了吗?
class Folio
  include Mongoid::Document
  include Mongoid::Timestamps::Updated

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