small medium large xlarge

 

Pt-E-1

Activity Description

Create a migration that copies the product price into the line item, and add_product method in the Cart model to capture the price whenever a new line item is created.

Author’s Solutions

blah

Readers’ Solutions blah

Marius says

The migration first: rails generate migration add_product_price_to_line_item price:decimal

class AddProductPriceToLineItem < ActiveRecord::Migration
  def self.up
    add_column :line_items, :price, :decimal, :precision => 8, :scale => 2
  end

  def self.down
    remove_column :line_items, :price
  end
end

In cart.rb

<code>
  def add_product(product_id, product_price)
    current_item = line_items.where(:product_id => product_id).first
    if current_item
      current_item.quantity += 1
    else
      current_item = LineItem.new(:product_id => product_id, :price => product_price)
      line_items << current_item
    end
    current_item
  end
</pre>
</code
Page History
  • V69: David Hislop [over 1 year ago]
  • V68: David Hislop [over 1 year ago]
  • V67: David Hislop [over 1 year ago]
  • V66: David Hislop [over 1 year ago]
  • V76: Steven Finnegan [over 4 years ago]
  • V75: Steven Finnegan [over 4 years ago]
  • V74: Steven Finnegan [over 4 years ago]
  • V73: Steven Finnegan [over 4 years ago]
  • V72: Duccio Armenise - P.IVA 01360060113 [over 4 years ago]
  • V71: Duccio Armenise - P.IVA 01360060113 [over 4 years ago]