Currently I have an Order class. each order has 1 to infinite number of items. the number of items is not known until run time. since there is no field type of Array in active record / rails, how do you create a variable number of columns?
The only way I can think of is to specify a bunch of ticket columns ahead of time; but is very inflexible and inefficient:
class CreateOrders < ActiveRecord::Migration
def self.up
t.integer :ticket_id, :ticket_id, :ticket_id, :ticket_id, :ticket_id
t.decimal :total
t.timestamps
end
def self.down
drop_table :orders
end
end
Typically, a 3rd Normal form of database table won’t have variable number of columns.
What you have can be 2 tables, one for order, and it “has many” line items, so this LineItem model will have entries stored in the line_items table, with each record having a product_id and quantity, and each LineItem “belongs to” an Order. (having a order_id, or line_item.order referring to the order it belongs to).