2011-10-30 5 views
1

私は新しい親を作成するときにモンゴイドが子オブジェクトのネストされた属性を設定しない理由を理解できないようです。新しいFolioを作成し、1つの子フィーチャを追加し、それをプロファイルのFolios配列にプッシュしたいとします。mongoidがnew()にネストされた属性を追加しないのはなぜですか?

私は多くの機能を埋め込む多くのフォリオを埋め込みプロファイルを、持っている:

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 

で作成、のparamハッシュはよさそうだ:

{"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 =()メソッドはありません。ネストされた属性が機能するためにはそれが必要だと思いましたか?私は何が欠けていますか?

これはMongoid 2.2.3のRails 3.1上です。

+0

の機能のために真の自動保存を有効にしようとしたがありますか? – jtomasrl

答えて

0

はあなたがこの作品を作るために管理したのにフォリオ文書

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

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

Worked。ドキュメントによれば、埋め込まれたドキュメントには必要ないはずです。また、attr_accessible:features_attributesをFolioに追加して機能させる必要がありました。 – ericvg

関連する問題