2009-07-08 11 views
1

を使用したとき、私は次のモデルがあります:「NameError:初期化されていない定数は、」Railsの多対多の関係

class Person < ActiveRecord::Base 
    has_many :accounts, :through => :account_holders 
    has_many :account_holders 
end 

class AccountHolder < ActiveRecord::Base 
    belongs_to :account 
    belongs_to :people 
end 

class Account < ActiveRecord::Base 
    has_many :people, :through => :account_holders 
    has_many :account_holders 
end 

この関係を使用している場合しかし、私は問題を取得しています。 Account.first.account_holdersはうまく動作しますが、Account.first.peopleが返されます。

NameError: uninitialized constant Account::People 
    from /Users/neil/workspace/xx/vendor/rails/activesupport/lib/active_support/dependencies.rb:105:in `const_missing' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/base.rb:2204:in `compute_type' 
    from /Users/neil/workspace/xx/vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/base.rb:2200:in `compute_type' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `send' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `klass' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:73:in `find_target' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:353:in `load_target' 
    from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:139:in `inspect' 

任意のアイデア?

答えて

5

belongs_toは、単数形が必要です。 AccountHolderで:

belongs_to :person 
1

私は同じ問題を持っていた答えは上記を示すように、それは私が単数に接合テーブルにbelongs_toのシンボル値を変更したとき(固定名の末尾に「s」を取り除いてしまいました)。私の練習では

、私が持っている:

guy.rb

class Guy < ActiveRecord::Base 
    attr_accessible :name 
    has_many :junctions 
    has_many :girls, through: :junctions 
end 

girl.rb

class Girl < ActiveRecord::Base 
    attr_accessible :name 
    has_many :junctions 
    has_many :guys, through: :junctions 
end 

junction.rb

class Junction < ActiveRecord::Base 
    attr_accessible :girl_id, :guy_id 
    belongs_to :girl # girls wouldn't work - needs to be singular 
    belongs_to :guy # guys wouldn't work - needs to be singular 
end 

この多対多くの関係はそれから...

関連する問題