has_one :through 是如何工作的?
-
01-07-2019 - |
题
我有三个模型:
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。
不隶属于 StackOverflow