From b475f556bb9e2e9978d73183161b524e2f0cf1d1 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Wed, 16 Nov 2016 19:53:09 +0100 Subject: [PATCH] Playtime Iteration E. --- app/models/cart.rb | 2 ++ db/migrate/20161116182902_add_price_to_line_items.rb | 5 +++++ db/migrate/20161116183040_fill_price_in_line_items.rb | 12 ++++++++++++ db/schema.rb | 9 +++++---- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20161116182902_add_price_to_line_items.rb create mode 100644 db/migrate/20161116183040_fill_price_in_line_items.rb diff --git a/app/models/cart.rb b/app/models/cart.rb index 6700a67..ff42767 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -8,6 +8,8 @@ class Cart < ApplicationRecord else current_item = line_items.build(product_id: product.id) end + # QUESTION: What happens if the price change in between to additions? + current_item.price = product.price current_item end diff --git a/db/migrate/20161116182902_add_price_to_line_items.rb b/db/migrate/20161116182902_add_price_to_line_items.rb new file mode 100644 index 0000000..061c779 --- /dev/null +++ b/db/migrate/20161116182902_add_price_to_line_items.rb @@ -0,0 +1,5 @@ +class AddPriceToLineItems < ActiveRecord::Migration[5.0] + def change + add_column :line_items, :price, :decimal, precision: 8, scale: 2 + end +end diff --git a/db/migrate/20161116183040_fill_price_in_line_items.rb b/db/migrate/20161116183040_fill_price_in_line_items.rb new file mode 100644 index 0000000..81663a3 --- /dev/null +++ b/db/migrate/20161116183040_fill_price_in_line_items.rb @@ -0,0 +1,12 @@ +class FillPriceInLineItems < ActiveRecord::Migration[5.0] + def up + LineItem.all.each do |item| + product = Product.find(item.product_id) + item.price = product.price + item.save! + end + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 85e47a6..1f3fa5a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161116161423) do +ActiveRecord::Schema.define(version: 20161116183040) do create_table "carts", force: :cascade do |t| t.datetime "created_at", null: false @@ -20,10 +20,11 @@ ActiveRecord::Schema.define(version: 20161116161423) do create_table "line_items", force: :cascade do |t| t.integer "product_id" t.integer "cart_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "quantity", default: 1 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "quantity", default: 1 t.integer "order_id" + t.decimal "price", precision: 8, scale: 2 t.index ["cart_id"], name: "index_line_items_on_cart_id" t.index ["order_id"], name: "index_line_items_on_order_id" t.index ["product_id"], name: "index_line_items_on_product_id"