मेरे पास तीन मॉडल हैं:Has_one कैसे है: काम के माध्यम से?
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
समस्या यह है कि, निष्पादन के दौरान, अगर मैं रिलीज_इटम में एक पैक जोड़ता हूं तो यह पता नहीं है कि पैक एक पैक है। उदाहरण के लिए:
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 तक पहुंच प्राप्त करनी चाहिए, लेकिन यह अनजान है कि पैक एक पैक आइटम है।
लेकिन अगर मैं has_many का उपयोग करता हूं: through, belong_to, या has_one, मुझे पहले सहेजने की ज़रूरत नहीं है। – lordscarlet
हाँ, ऐसा लगता है कि इसका ख्याल रखा गया है। मुझे नहीं लगता कि मुझे अन्य संगठनों के साथ ऐसा करना है, हालांकि। – lordscarlet
क्या इसका ख्याल रखना है? क्या आप अपने इंटरैक्टिव कंसोल सत्र की एक क्लिप पोस्ट कर सकते हैं? मैंने रेल 2.1.0 का उपयोग करके थोड़ा परीक्षण आवेदन किया और यहां तक कि पहले से ही बचत की स्थिति में मदद नहीं की, इसलिए मैंने अपना जवाब सबमिट कर दिया। –