I’m trying to do a “ecommerce like” solution. When a product is defined, there are many option_values, e.g. whip cream => yes.
When I’m creating a line_item, my associations are like this.
class LineItem < ActiveRecord::Base
belongs_to :cart
belongs_to :order
belongs_to :product
belongs_to :shop
...
When a user chooses a product, he chooses options_values and a line_item must be created to store this.
Currently I’m not sure what is a good way to model the option_values in a line item.
My gut instinct is that a line_item has_many option_values, but it would seem weird to store foreign keys in option_values. The other thought is that line_item habtm option_values.
What would be a good way to model this line_item has_many option_values relationship, ideally storing the reference in the line_items table?
If your
option_valuesare always boolean (whip cream => yes) you should have anOptionValuesmodel with all availableoption_valuesand do habtm betweenLineItemandOptionValues.If your option values are not always boolean (sugar => 2.spoons) then you must store this value (2.spoons) in the join model, so you should do something like this:
And have the table for
LineItemOptionValuea field for the value of the option.