has_one:通过工作怎么样?

时间:2008-09-18 04:04:20

标签: ruby-on-rails ruby activerecord

我有三种模式:

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项目。

3 个答案:

答案 0 :(得分:6)

您对has_one:through的使用似乎是正确的。您看到的问题与保存对象有关。要使关联起作用,正在引用的对象需要具有id来填充对象的model_id字段。在这种情况下,PackReleaseItems需要填充pack_idrelease_item_id字段才能使关联正常工作。在通过关联访问对象之前尝试保存。

答案 1 :(得分:2)

您的问题在于如何关联ReleaseItemPack

has_many :throughhas_one :through都通过一个也充当连接表的对象,在本例中为PackReleaseItem。由于这不仅仅是一个连接表(如果是,你应该只使用has_many而没有:through),正确创建关联需要创建连接对象,如下所示:

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

您在item.pack = pack电话中所做的只是简单地关联内存中的对象。当您再次查找时,它会显示“throughpack_release_items,这是空的。

答案 2 :(得分:1)

您想要保存或创建(而不是新)项目和包。否则,数据库尚未为关联分配id。