Ich speichere Daten zu parlamentarischen Angelegenheiten in meiner Datenbank als Affair
, die einem gehören können Councillor
oder ein Group
. Ich bin neu bei Ruby und ActiveRecord. In früheren RoR-Versionen hätte ich verwendet has_and_belongs_to_many
und es hat auch funktioniert.
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
Später im Code möchte ich eine neue Vereinigung gründen:
affair.councillors << Councillor.find_by(:id => 3)
Aber aus irgendeinem Grund funktioniert es nicht. Es gibt eine Fehlermeldung:
/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>"
Was ist meine Schuld? Wie kann ich dieses Problem lösen? Danke fürs Helfen.
Antworten:
3 für die Antwort № 1Der Klassenname sollte sein AffairHolding
und nicht Affair_Holdings
.
0 für die Antwort № 2
Ja, der Klassenname sollte "AffairHolding" sein und für den Klassennamen immer nur Großbuchstaben verwenden