Skip to content

Instantly share code, notes, and snippets.

@avanishgiri
Created October 3, 2013 08:03
Show Gist options
  • Select an option

  • Save avanishgiri/6806697 to your computer and use it in GitHub Desktop.

Select an option

Save avanishgiri/6806697 to your computer and use it in GitHub Desktop.
solutions
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.integer :seller_id
t.timestamps
end
end
end
class CreateSellers < ActiveRecord::Migration
def change
create_table :sellers do |t|
t.string :name
t.decimal :balance
t.decimal :total_sales
t.timestamps
end
end
end
class CreatePurchases < ActiveRecord::Migration
def change
create_table :purchases do |t|
t.decimal :amount
t.boolean :already_paid
t.integer :product_id
t.timestamps
end
end
end
class AddIndexes < ActiveRecord::Migration
def change
add_index :products, :seller_id
add_index :purchase, :product_id
end
end
class Seller < ActiveRecord::Base
has_many :products
has_many :purchases, :through => :products
end
class Product < ActiveRecord::Base
belongs_to :seller
has_many :purchases
end
class Purchase < ActiveRecord::Base
belongs_to :product
has_one :seller, :through => :product
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment