次の 3 つのモデルがあります。
class Group < ActiveRecord::Base
attr_accessible :name
has_many :users
has_and_belongs_to_many :suppliers,
:class_name => "Group",
:foreign_key => "customer_id",
:association_foreign_id => "supplier_id"
has_and_belongs_to_many :customers,
:class_name => "Group",
:foreign_key => "supplier_id",
:association_foreign_id => "customer_id"
has_many :orders, :as => :orderable
validates :name => :presence => true
end
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :orders, :as => :orderable
belongs_to :group
validates :email, :name, :group_id, :presence => true
end
class Order < ActiveRecord::Base
belongs_to :orderable, :polymorphic => true
validates :orderable_id, :presence => true
end
自己結合Groupモデルによれば、グループには と の 2 つの「タイプ」がcustomersありsuppliersます。
ここで、グループ タイプhas_many :orders, :as => :orderableに対してのみ関連付けが存在し、 に対しては存在しないようにします。customersuppliers
したがって、 のみcustomerが多くの注文を持つことができ、 は関連付けるsupplierことができませんOrder。
これを達成する方法はありますか?または、GroupモデルをCustomerとSupplierモデルに分割する必要がありますか?
ありがとう!