I currently have some models: User, Dealer, Sale and Role. Role has a polymorphic belongs_to relationship with Dealer and Sale, and belongs_to User (see code below).
My query is this: how can I specify a has_many :dealers, :through => :roles
relationship on User for Dealer and Sale? The role model that the User model would be associating through belongs_to either a Dealer or a Sale, and as such the relationship in this format does not work.
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
Would appreciate any assistance here.
发现:source选项后,我能够解决此问题-参见http://guides.rubyonrails.org/association_basics.html#has_many-association-reference (第4.3.2.20部分)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.