开发者

Rails has_many :through

开发者 https://www.devze.com 2023-04-05 13:06 出处:网络
I\'m trying to create a point of sale app in rails Right now I have an items model. The items model contains each of the different items carried by the store.

I'm trying to create a point of sale app in rails

Right now I have an items model. The items model contains each of the different items carried by the store.

Now I want to create a Orders model. The idea is that this model would contain the items and their quantity for each order. I would be able to view the history of orders to generate开发者_Python百科 analytics etc.

What is the right way to do this?

has_many :through ? Redis sets? Other ideas?


The typical approach is to use an OrderLine model. This is where you would store the quantity of each item ordered.

class Order
  has_many :order_lines
  has_many :items, :through => :order_lines
end

class OrderLine
  belongs_to :order
  belongs_to :item
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号