【发布时间】:2014-03-11 13:22:11
【问题描述】:
我正在考虑一种模式,用户可以将产品添加到他的订单列表中,并且该订单将具有状态、评论等。多个用户可以有多个订单。每个订单都有一个产品。最好的方法是什么?
class User
has_many :orders
end
class Order
has_one :product
end
class Product
belongs_to :category
has_and_belongs_to_many :orders
end
或者我应该考虑使用 has_many :through 吗?另外,has_one 关联与 Product 模型具有的 habtm 关联有冲突,还是可以这样使用它?谢谢!
UPD1:经过大量实验后,我选择了以下内容:
class Order
belongs_to :user
belongs_to :product
end
class Product
belongs_to :category
has_many :orders
end
class User
has_many :orders
end
虽然目前一切正常,但我很想听听任何 cmets 或建议
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 associations