Last active
September 10, 2015 19:44
-
-
Save halilim/53aa2402fb2321f3ab27 to your computer and use it in GitHub Desktop.
Bare bones e-commerce Rails models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
has_many :order_items | |
has_many :products, through: :order_items | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderItem < ActiveRecord::Base | |
belongs_to :order | |
belongs_to :product | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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