1

명백히 새로운 부모 레코드를 인스턴스화하는 동안 중첩 된 연관 레코드를 만들려고하는 아래에서 수행 한 예제에서 매우 단순한 것을 간과하고 있습니다.중첩 된 연관 레코드 만들기

나는 새로운 도움을 원한다면 내게 며칠 동안 먹고있는 것을 찾아야한다. 미리 감사드립니다! 내가 누락되었거나 엉망이 되었습니까? 그렇게 사소한 것처럼 보인다.

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' 

희망 사항이 수정 될 수 있기를 바랍니다. 내 부분에 명백한 실수가 있으면 죄송합니다.

답변

3

글쎄, 정말 어리 석고 오류 로그는 전혀 도움이 안된다. 그래서, 해쉬 안에서 중첩 된 모든 연관을 인스턴스화하기 위해 Model.new()로 전달할 수있는 해쉬를 망쳐 놓았습니다 (레일즈 의사는 대다수의 시간이 매우 좋습니다).

당신이 순서에 해시의 배열에 키를 할당해야하지만, 내가 문서화되지 않은 것 같다 발견 무엇을한다는 것입니다 (누군가의 올바른 방향으로 날 가리 할 수있는 경우를 제외하고는 문서의 위치입니다)

(예 레일 본체) 부정확

:

,174 올바르게 새로운 하위 연결 레코드를 인스턴스화 올바른
> p = {"contents_attributes"=>{"sort_item_attributes"=>{"position"=>"1"}}} 

:

> p = {"contents_attributes"=>[{"sort_item_attributes"=>{"position"=>"1"}}]} 

공지 아주 미묘한 차이 : 당신이 중첩 된 협회의 키가 필요, - "contents_attributes", 배열 점 - [{ "sort_item_attributes "=> {"position "=>"1 "}}], 이는 content_groups이기 때문에,이 경우 has_many 내용입니다.

콘텐츠 has_one sort_item의 has_one 관계와 같은 has_one 관계에는 이러한 필요성이 없습니다.

이론적으로 이것은 완전한 의미를 갖지만 오류 로그는 내가 가진 문제를 해결하기위한 좋은 방향으로는 나를 가리 키지 않았으며 레일 의사는 부족한 것처럼 보였습니다. 결국 nested_attributes.rb 코드/examples 행 93-95 중 일부를 읽어야했습니다. 또한 그것은 내가이 문제에 대해 빌려준 아약스 작업을하는 것을 돕지 못했고, 그래서 나의 뇌는 조금 벗어났다.

다행스럽게도 필자는 누군가가 같은/비슷한 문제에 대한 신속한 답변을 찾는 데 도움이됩니다 이것에 내 머리를 두드리는 소요되는 시간.