문제

세 가지 모델이 있습니다.

class ReleaseItem < ActiveRecord::Base
  has_many :pack_release_items
  has_one :pack, :through => :pack_release_items
end

class Pack < ActiveRecord::Base
  has_many :pack_release_items
  has_many :release_items, :through=>:pack_release_items
end

class PackReleaseItem < ActiveRecord::Base
  belongs_to :pack
  belongs_to :release_item
end

문제는 실행 중에 release_item에 팩을 추가하면 해당 팩이 팩이라는 것을 인식하지 못한다는 것입니다.예를 들어:

Loading development environment (Rails 2.1.0)
>> item = ReleaseItem.new(:filename=>'MAESTRO.TXT')
=> #<ReleaseItem id: nil, filename: "MAESTRO.TXT", created_by: nil, title: nil, sauce_author: nil, sauce_group: nil, sauce_comment: nil, filedate: nil, filesize: nil, created_at: nil, updated_at: nil, content: nil>
>> pack = Pack.new(:filename=>'legion01.zip', :year=>1998)
=> #<Pack id: nil, filename: "legion01.zip", created_by: nil, filesize: nil, items: nil, year: 1998, month: nil, filedate: nil, created_at: nil, updated_at: nil>
>> item.pack = pack
=> #<Pack id: nil, filename: "legion01.zip", created_by: nil, filesize: nil, items: nil, year: 1998, month: nil, filedate: nil, created_at: nil, updated_at: nil>
>> item.pack.filename
NoMethodError: undefined method `filename' for #<Class:0x2196318>
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1667:in `method_missing_without_paginate'
    from /usr/local/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.3/lib/will_paginate/finder.rb:164:in `method_missing'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb:285:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb:285:in `method_missing_without_paginate'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1852:in `with_scope'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:168:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:168:in `with_scope'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb:281:in `method_missing_without_paginate'
    from /usr/local/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.3/lib/will_paginate/finder.rb:164:in `method_missing'
    from (irb):5
>> 

item.pack에 접근해야 할 것 같은데, 해당 팩이 Pack 아이템인지는 인식하지 못하고 있습니다.

도움이 되었습니까?

해결책

has_one :through 사용법이 올바른 것 같습니다.당신이 보고 있는 문제는 객체 저장과 관련이 있습니다.연결이 작동하려면 참조되는 개체에 개체를 채울 ID가 있어야 합니다. model_id 개체에 대한 필드입니다.이 경우, PackReleaseItems 가지고있다 pack_id 그리고 release_item_id 연결이 올바르게 작동하려면 채워야 하는 필드입니다.연결을 통해 개체에 액세스하기 전에 저장해 보세요.

다른 팁

문제는 연결 방법에 있습니다. ReleaseItem 그리고 Pack.

has_many :through 그리고 has_one :through 둘 다 조인 테이블 역할도 하는 개체를 통해 작동합니다. 이 경우 PackReleaseItem.이것은 단순한 조인 테이블이 아니기 때문에(만약 그렇다면 다음을 사용해야 합니다. has_many 없이 :through), 연결을 올바르게 생성하려면 다음과 같이 조인 개체를 생성해야 합니다.

>> item.pack_release_items.create :pack => pack

당신은 무엇을하고 있습니까? item.pack = pack 호출은 단순히 메모리의 개체를 연결하는 것입니다.다시 찾아보면 "through" pack_release_items, 이는 비어 있습니다.

항목과 팩을 저장하거나 (새로 만드는 대신) 만들고 싶습니다.그렇지 않으면 데이터베이스가 연결에 대한 ID를 할당하지 않은 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top