2012-08-10 2 views
0

현재 사용자, 딜러, 판매 및 역할 중 일부 모델이 있습니다. 역할은 Dealer와 Sale 및 belongs_to User와의 다형성 belongs_to 관계를가집니다 (아래 코드 참조).레일즈 - 다형성 연관에서 부모에 대해 has_many를 지정합니다.

내 검색어는 다음과 같습니다. has_many :dealers, :through => :roles 사용자를 대리점 및 판매용으로 지정하려면 어떻게해야합니까? 사용자 모델이 belongs_to 딜러 또는 판매와 연결될 역할 모델이므로이 형식의 관계는 작동하지 않습니다.

class User < ActiveRecord::Base 
    has_many :roles 
    has_many :sales, :through => :roles 
    has_many :appraisals, :through => :roles 
    has_many :dealers, :through => :roles 
end 

class Dealer < ActiveRecord::Base 
    has_many :roles, :as => :role_originator 
    has_many :users, :through => :roles 
end 

class Sale < ActiveRecord::Base 
    has_many :roles, :as => :role_originator 
    has_many :users, :through => :roles 
end 

class Role < ActiveRecord::Base 
    belongs_to :role_type 
    belongs_to :user 
    belongs_to :role_originator, :polymorphic => true 
end 

여기에서 도움을 받으실 수 있습니다.

답변

관련 문제