Add many to many relation between orders and products
This commit is contained in:
parent
2575133ce2
commit
60bf8030e5
7 changed files with 44 additions and 2 deletions
|
@ -1,5 +1,8 @@
|
|||
class Order < ActiveRecord::Base
|
||||
belongs_to :users
|
||||
has_many :order_products
|
||||
has_many :products, through: :order_products
|
||||
|
||||
default_scope -> { order('created_at DESC') }
|
||||
|
||||
end
|
||||
|
|
4
app/models/order_product.rb
Normal file
4
app/models/order_product.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class OrderProduct < ActiveRecord::Base
|
||||
belongs_to :order
|
||||
belongs_to :product
|
||||
end
|
5
db/migrate/20141125130213_drop_products_on_users.rb
Normal file
5
db/migrate/20141125130213_drop_products_on_users.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class DropProductsOnUsers < ActiveRecord::Migration
|
||||
def change
|
||||
remove_column :orders, :products
|
||||
end
|
||||
end
|
8
db/migrate/20141125130447_create_order_products.rb
Normal file
8
db/migrate/20141125130447_create_order_products.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
class CreateOrderProducts < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :order_products do |t|
|
||||
t.belongs_to :order
|
||||
t.belongs_to :product
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,11 +11,15 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20141125102501) do
|
||||
ActiveRecord::Schema.define(version: 20141125130447) do
|
||||
|
||||
create_table "order_products", force: true do |t|
|
||||
t.integer "order_id"
|
||||
t.integer "product_id"
|
||||
end
|
||||
|
||||
create_table "orders", force: true do |t|
|
||||
t.integer "user_id"
|
||||
t.string "products"
|
||||
t.integer "cost"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
|
11
test/fixtures/order_products.yml
vendored
Normal file
11
test/fixtures/order_products.yml
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
7
test/models/order_product_test.rb
Normal file
7
test/models/order_product_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OrderProductTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in a new issue