From 60bf8030e5be0bf8f98fb94dac0022b619abf0f8 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Tue, 25 Nov 2014 14:27:27 +0100 Subject: [PATCH] Add many to many relation between orders and products --- app/models/order.rb | 3 +++ app/models/order_product.rb | 4 ++++ db/migrate/20141125130213_drop_products_on_users.rb | 5 +++++ db/migrate/20141125130447_create_order_products.rb | 8 ++++++++ db/schema.rb | 8 ++++++-- test/fixtures/order_products.yml | 11 +++++++++++ test/models/order_product_test.rb | 7 +++++++ 7 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 app/models/order_product.rb create mode 100644 db/migrate/20141125130213_drop_products_on_users.rb create mode 100644 db/migrate/20141125130447_create_order_products.rb create mode 100644 test/fixtures/order_products.yml create mode 100644 test/models/order_product_test.rb diff --git a/app/models/order.rb b/app/models/order.rb index 6b5f99d..7e419af 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/order_product.rb b/app/models/order_product.rb new file mode 100644 index 0000000..2e61e64 --- /dev/null +++ b/app/models/order_product.rb @@ -0,0 +1,4 @@ +class OrderProduct < ActiveRecord::Base + belongs_to :order + belongs_to :product +end diff --git a/db/migrate/20141125130213_drop_products_on_users.rb b/db/migrate/20141125130213_drop_products_on_users.rb new file mode 100644 index 0000000..70e806f --- /dev/null +++ b/db/migrate/20141125130213_drop_products_on_users.rb @@ -0,0 +1,5 @@ +class DropProductsOnUsers < ActiveRecord::Migration + def change + remove_column :orders, :products + end +end diff --git a/db/migrate/20141125130447_create_order_products.rb b/db/migrate/20141125130447_create_order_products.rb new file mode 100644 index 0000000..dcb2194 --- /dev/null +++ b/db/migrate/20141125130447_create_order_products.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index d2a68be..b7077ab 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/test/fixtures/order_products.yml b/test/fixtures/order_products.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/order_products.yml @@ -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 diff --git a/test/models/order_product_test.rb b/test/models/order_product_test.rb new file mode 100644 index 0000000..289c5a8 --- /dev/null +++ b/test/models/order_product_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class OrderProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end