0

我正在寻找一个名为 Products 的资源。产品有描述/价格...

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

这一切都很简单,但我可能有 2 个单独的“奶油奶酪”容器,它们有不同的保质期。围绕这些单个变量对资源进行建模的正确/最佳方法是什么。

编辑:对不起,上面不是很清楚。我将收到许多相同的奶油奶酪订单,具有相同的 UPC 代码和价格,所以我认为将其作为单一产品会很好。然后我需要以某种方式存储单个项目的到期日期,也许通过单独的相关资源?

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 回答