Rails has_many :through
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
精彩评论