簡體   English   中英

Rails-為多態關聯中的父級指定has_many

[英]Rails - specifying has_many for a parent in polymorphic association

我目前有一些模型:用戶,經銷商,銷售和角色。 角色與經銷商和銷售,以及與用戶之間具有多態的belongs_to關系(請參見下面的代碼)。

我的查詢是這樣的:我如何在經銷商和銷售用戶上指定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

希望在這里提供任何幫助。

發現:source選項后,我能夠解決此問題-參見http://guides.rubyonrails.org/association_basics.html#has_many-association-reference (第4.3.2.20部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM