Я не могу понять, почему 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"=>{"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.