0

契約、サービス、価格の 3 つのモデルがあります。

class Agreement < ActiveRecord::Base
  has_many :prices, as: :priceable
end

class Service < ActiveRecord::Base
  has_many :prices, as: :priceable
end

class Price  < ActiveRecord::Base
  attr_accessible :price, :price_currency, :priceable_id, :priceable_type
  belongs_to :priceable, polymorphic: true
end

しかし、サービス customer_price と Agency_price には 2 つの価格タイプがあります。契約に価格タイプがありません。以下のようなものをモデル化したいと思います。それはどのように可能ですか?

class Agreement < ActiveRecord::Base
  has_many :prices, as: :priceable
end

class Service < ActiveRecord::Base
  has_many :customer_prices, as: :priceable # I think I should write something here
  has_many :agency_prices, as: :priceable   # I think I should write something here
end

class Price  < ActiveRecord::Base
  attr_accessible :price, :price_currency, :priceable_id, :priceable_type
  belongs_to :priceable, polymorphic: true
end

ベストアプローチとは?AgreementPrice と ServicePrice のような 2 つの価格モデルを作成する必要があるかもしれません。よろしくお願いします。

4

1 に答える 1

0

2 つのモデルを作成しても、実際には役に立ちません。

リレーションに特定のforeign_keyを設定するだけでよいと思います。

has_many :customer_price, as: :priceable, :foreign_key => customer_price_id
has_many :agency_price, as: :priceable, :foreign_key => agency_price_id

これら 2 つのフィールドをスキーマに追加する必要があります。

于 2013-03-06T16:41:16.400 に答える