显然,在下面的示例中,我忽略了一些非常简单的事情,我试图在实例化新父记录时创建嵌套关联记录。
我希望有一双新的眼睛能帮助我找到这些天来一直困扰着我的东西。提前致谢!我错过了什么/搞砸了什么?似乎如此微不足道。
ActiveRecord::NestedAttributes 显然不高兴。
class ContentGroup < ActiveRecord::Base
attr_protected
has_many :contents, :dependent=>:destroy
accepts_nested_attributes_for :contents
end
class Content < ActiveRecord::Base
attr_protected
has_one :sort_item, :as=>:sortable
belongs_to :content_group, :dependent=>:destroy
accepts_nested_attributes_for :sort_item
end
class SortItem < ActiveRecord::Base
attr_accessible :position, :sortable_id, :sortable_type
belongs_to :sortable, :polymorphic=>true
end
在 Rails 控制台中,轻度嵌套调用按预期工作:
> p = {"sort_item_attributes"=>{"position"=>"1"}}
> b = Content.new(p)
=> Content id: nil, content_group_id: nil
又炸了一窝:
> p = {"contents_attributes"=>{"sort_item_attributes"=>{"position"=>"1"}}}
> cg = ContentGroup.new(p)
ActiveRecord::UnknownAttributeError: unknown attribute: position
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/reflection.rb:183:in `new'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/reflection.rb:183:in `build_association'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:233:in `build_record'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:112:in `build'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:406:in `block in assign_nested_attributes_for_collection_association'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:401:in `each'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:401:in `assign_nested_attributes_for_collection_association'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:289:in `contents_attributes='
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:93:in `each'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
from (irb):10:in `new'
from (irb):10
from /.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
希望这可以解决,如果我有明显的错误,请见谅。