2009-07-08 3 views
1

나는 다음과 같은 모델을 가지고 :"나가서 설명하자면 NameError : 초기화되지 않은 상수"때 사용하는 레일 '다 대다 관계

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_holder는 정상적으로 작동하지만 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 

이 많은 대 많은 관계가 작동합니다 ...

관련 문제