मैं अपने डेटाबेस में संसदीय मामलों के बारे में डेटा सहेजता हूं Affair
, जो एक से संबंधित हो सकता है Councillor
या ए Group
। मैं रूबी और ActiveRecord के लिए नया हूँ। रो के पूर्व संस्करणों में मैंने इस्तेमाल किया होगा has_and_belongs_to_many
, और यह भी काम किया।
class Councillor < ActiveRecord::Base
has_many :affair_holdings, :foreign_key => :councillor_id
has_many :affairs, through: :affair_holdings
end
class Affair < ActiveRecord::Base
has_many :affair_holdings, :foreign_key => :affair_id
has_many :councillors, through: :affair_holdings
end
class Affair_Holdings < ActiveRecord::Base
belongs_to :affair
belongs_to :councillor
end
बाद में कोड में, मैं एक नया संघ स्थापित करना चाहता हूं:
affair.councillors << Councillor.find_by(:id => 3)
लेकिन किसी भी कारण से यह काम नहीं करता है। यह मुझे एक त्रुटि संदेश देता है:
/Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/inheritance.rb:125:in `compute_type": uninitialized constant Affair::AffairHolding (NameError)
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/reflection.rb:178:in `klass"
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:123:in `klass"
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/collection_association.rb:37:in `reader"
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `affair_holdings"
from crawl.rb:196:in `affair"
from crawl.rb:233:in `<main>"
मेरी गल्ती क्या है? इस समस्या का समाधान किस प्रकार से किया जा सकता है? मदद के लिए धन्यवाद।
उत्तर:
जवाब के लिए 3 № 1वर्ग का नाम होना चाहिए AffairHolding
और नहीं Affair_Holdings
.
जवाब के लिए 0 № 2
हां कक्षा का नाम अफेयरहोल्डिंग होना चाहिए और हमेशा वर्ग नाम के लिए एकवचन और प्रारंभिक पूंजी अक्षरों का उपयोग करना चाहिए