명백히 새로운 부모 레코드를 인스턴스화하는 동안 중첩 된 연관 레코드를 만들려고하는 아래에서 수행 한 예제에서 매우 단순한 것을 간과하고 있습니다.중첩 된 연관 레코드 만들기
나는 새로운 도움을 원한다면 내게 며칠 동안 먹고있는 것을 찾아야한다. 미리 감사드립니다! 내가 누락되었거나 엉망이 되었습니까? 그렇게 사소한 것처럼 보인다.
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
는 가볍게 중첩 된 호출은 예상대로 작동합니다
이> 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'
희망 사항이 수정 될 수 있기를 바랍니다. 내 부분에 명백한 실수가 있으면 죄송합니다.