Skip to content

Instantly share code, notes, and snippets.

@halilim
Last active September 10, 2015 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halilim/53aa2402fb2321f3ab27 to your computer and use it in GitHub Desktop.
Save halilim/53aa2402fb2321f3ab27 to your computer and use it in GitHub Desktop.
Bare bones e-commerce Rails models
class CreateTables < ActiveRecord::Migration
def change
create_table :products do |t|
# ...
end
create_table :orders do |t|
# ...
end
create_table :order_items do |t|
t.references :order, index: true, foreign_key: true
t.references :product, index: true, foreign_key: true
t.integer :quantity
# ...
end
end
end
class Order < ActiveRecord::Base
has_many :order_items
has_many :products, through: :order_items
end
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :order_items
has_many :orders, through: :order_items
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment