ruby on rails - Database design for optional fields -


i've got 3 tables:

  1. orders
  2. line_items
  3. products

they're setup as:

order has many products through line_items

this allows me store in line_items such things product_id, quantity, price @ time of purchase, discount, etc...

all point.

what i'm looking achieve:

i need have some products have user changeable status. meaning @ point in future after order has been processed, purchased product status can changed 1 status another.

the product table has statusable boolean field tracks whether said product supports status.

the question:

would add status field in line_items? small amount of products require status feels waste i'm not sure how else approach hurdle. main concern being i'll end massive table application grows , specific products require optional fields.

there 2 options this:

  1. create column in join model
  2. create separate "statuses" table, can use create specific status updates

--

attribute

i create column in join model support status. yes, require have column every line_item, make process simpler, , give extensibility without massive issues

you'll best using 1 of state machine gems (state_machine, aasm_state) provide:

#app/models/line_item.rb class lineitem < activerecord::base   include aasm    aasm     state :active, :initial => true     state :inactive      event :activate       transitions :from => :inactive, :to => :active     end      event :deactivate       transitions :from => :active, :to => :inactive     end   end end 

this give ability directly affect status of line_item model

--

associated model

alternatively, may want create different table / model:

#app/models/line_item_status.rb class status < activerecord::base    #field id | line_item_id | status | created_at | updated_at    belongs_to :line_item end  #app/models/line_item.rb class lineitem < activerecord::base    has_one :status    delegate :status, to: :status #-> allows call @line_item.status end 

this give ability set status each product, making data tables more efficient including single status each line_item


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -