0
我目前有一些模型:User,Dealer,Sale和Role。角色与经销商和销售以及belongs_to用户具有多态的belongs_to关系(请参阅下面的代码)。Rails - 为多态关联中的父项指定has_many
我的查询是这样的:我怎么能指定一个has_many :dealers, :through => :roles
关系经销商和销售用户?用户模型将与之关联的角色模型属于经销商或销售,因此此格式的关系不起作用。
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
希望在这里得到任何帮助。