Want to receive a weekly email containing
the scoop on our new titles along with the
occasional special offer? Just click the button.
(You can always unsubscribe later by editing your
account information).

Give us an email and a password (you can use the password later to log in and
change your preferences). We'll send you a newsletter roughly once a week.

Doc says

Through unit testing, I found that the price and quantity are not actually updated in the table for data added after the migration. I changed the add_product method to include a parameter for the current price (defaults to nil to use the catalog price if current_price is not provided) and used the update_attributes method to update the table with the new price and quantity. I chose to update the price any time the line item is changed.

Will says

In the examples given above, why is the “current_item” added to “line_items”? This code is not included in the book?

File: app/models/cart.rb

def add_product(product_id)
..
line_items << current_item

Am I correct that the save method in “line_items_controller” already covers this?

File: app/controllers/line_items_controller.rb

def create
..
if @line_item.save

Greg says

Am I missing something, or you overcomplicate the code of the Cart model?

I added one line to the add_product method:

File: app/models/cart.rb

current_item.price = current_item.product.price

Karen says

This last proposed solution won’t work if current_item does not
exist yet (the current item is not the same book as an item already in the cart.)
You get a method not found error for price on the current item.

You do need to go back using the product_id to get the matching
product and price if current_item does not exist…

David says:

(Note that I used Rails 4.0.0)

The wording of the Activity Description hints at a simple implementation of what could be a more complex exercise. Capturing the price whenever a new line item is created means that the cart price is set to the product price at the time of the first Add to Cart operation. Even if you change the product price after this, subsequent additions of the same product to the same cart will use the price stored in the cart. This is probably the minimum behaviour that a customer would expect.

You can make this more complicated by an ambitious interpretation of the Activity Description. For example, you could create a new line item for every different price for each product item, but changing prices during purchase is unlikely to win friends in the customer community. Even more ambitious, you could maintain prices at the same value during a customer session, but we would need a more complex data model incorporating datetime-based product pricing.

The method I’ve used below assumes the same data model as in the book and that we set the cart line item price for any product from the product price at the time of the first Add to Cart operation for that line item.

Edit the resulting migration file: The migration generation command will create a file matching the pattern db/migrate/*_add_product_price_to_line_items.rb. Edit that file, or the most recent file matching the pattern if there is more than one, to look like this: