0

Products というリソースを作成しようとしています。製品には説明/価格があります...

{
  description: "Cream Cheese",
  price: 1.99,
  perishable: true
}

単純な話ですが、「クリームチーズ」の賞味期限が違う容器が2つ在庫があるかもしれません。これらの個々の変数を中心にリソースをモデル化するための正しい/最良の方法は何ですか?

編集:申し訳ありませんが、上記はあまり明確ではありませんでした。同じCream Cheeseで同じUPCコード、同じ価格でたくさん注文が入るので、1つの商品にした方がいいと思います。次に、おそらく別の関連リソースを介して、個々のアイテムの有効期限を保存する必要がありますか?

4

1 に答える 1

0

あなたは問題をかなりうまく説明していると思います (これは通常、難しい部分です)。あとは、書いたものをモデル化するだけです。保存する必要があるものに応じて、多くの選択肢があります。あなたの本当のケースを刺激するかもしれないものを提案します:

class Product < ActiveRecord::Base
  has_many :items

  # upc: string
  # string: description string
  # float: price (or integer representing cents)
  # boolean: perishable
end

class Item < ActiveRecord::Base
  belongs_to :product

  # datetime: expires_at
end

class OrderItem < ActiveRecord::Base
  belongs_to :item
  belongs_to :order

  # integer: quantity
end

class Order < ActiveRecord::Base
  has_many :order_items
end
于 2013-08-27T21:09:52.997 に答える