1
我有3種型號,用戶,經銷商和角色,看起來如下:導軌 - 中的has_many協會限制結果
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
的關聯是否按預期工作 - 例如,我可以做dealer.users
並得到所有在該經銷商有角色的用戶。
我遇到的問題是我希望能夠做到dealer.users.first.roles
並只返回與該經銷商相關聯的角色,但目前它返回與用戶相關聯的所有角色,無論是否他們指的是另一個經銷商。
如何修改我的模型以允許dealer.users.first.roles
僅返回與所選經銷商和用戶關聯的角色,而不是用戶的所有角色?