0

小さなRailsアプリでShiping Zonesを作成するという問題を回避しようとしています。

データベースには、他のユーザーに販売できるユーザーに属するアイテムがリストされます。どちらも世界規模であり、原産国は location_countries テーブルにリストされています。

私が現在これを見ている方法は、配送ゾーンのテーブル [id, name, description] とマッピング テーブル [from_country_id, to_country_id, shipping_zone_id] があり、使用するゾーン ルールを決定するために location_countries テーブルを 2 回参照し、 shipping_zones テーブル (最終的に価格表を参照するため)。

私が抱えている問題は、これがモデル ファイルでどのように表現されるべきかを考え出すことです: location_country は、別の location_country に応じて複数のゾーンを持つことができます。

これは簡単なことのように思えますが、私はこれをすべて間違って扱っているようにも感じます. これについての指針はありますか?

4

1 に答える 1

1

次のようにクラスを作成してリンクすることをお勧めします。

class Country

  has_many :from_mappings, :class_name => "Mapping", :foreign_key => :from_country_id
  has_many :from_countries, :through => :from_mappings

  has_many :to_mappings, :class_name => "Mapping", :foreign_key => :to_country_id
  has_many :to_countries, :through => :to_mappings
...

class Mapping

  belongs_to :shipping_zone

  belongs_to :from_country, :class_name => "Country", :foreign_key => :to_country_id
  belongs_to :to_country, :class_name => "Country", :foreign_key => :from_country_id
...

それが解決策になるはずですが、問題の一部を誤解している可能性があります

于 2012-09-19T13:24:24.770 に答える