Browse Source

Playtime Iteration E.

master
Nils Dittberner 9 years ago
parent
commit
b475f556bb
4 changed files with 24 additions and 4 deletions
  1. +2
    -0
      app/models/cart.rb
  2. +5
    -0
      db/migrate/20161116182902_add_price_to_line_items.rb
  3. +12
    -0
      db/migrate/20161116183040_fill_price_in_line_items.rb
  4. +5
    -4
      db/schema.rb

+ 2
- 0
app/models/cart.rb View File

@@ -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



+ 5
- 0
db/migrate/20161116182902_add_price_to_line_items.rb View File

@@ -0,0 +1,5 @@
class AddPriceToLineItems < ActiveRecord::Migration[5.0]
def change
add_column :line_items, :price, :decimal, precision: 8, scale: 2
end
end

+ 12
- 0
db/migrate/20161116183040_fill_price_in_line_items.rb View File

@@ -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

+ 5
- 4
db/schema.rb View File

@@ -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"


Loading…
Cancel
Save