small medium large xlarge

Img_2084_pragsmall
08 Sep 2010, 19:22
Stuart Gale (6 posts)

Hi folks,

I’m experiencing something really strange, but I can’t figure out what’s going on. I’ve applied the migration that sets the line item price for existing entries in the database and I’m now trying to get the code to work when adding new items. Inside the add_product method in the cart model I have the following:

    else
      current_item = LineItem.new(:product_id => product_id)
      current_item.price = current_item.product.price # new statement
      line_items << current_item
    end

The new statement causes:

test_should_create_line_item(LineItemsControllerTest):
NoMethodError: undefined method `price=' for #<LineItem:0x00000101ed21a8>

Any ideas?

Cheers, Stuart

Samr_small_pragsmall
09 Sep 2010, 01:14
Sam Ruby (634 posts)

It doesn’t look like the migration “took” somehow. Take a peek inside db/schema.rb; I’ll bet that table “line_items” doesn’t have a price.

Img_2084_pragsmall
09 Sep 2010, 13:20
Stuart Gale (6 posts)

I checked db/schema.rb, “line_items” table did have a price…

I decided to revert the codebase back to the previous commit and create the migration from scratch.

In the original post I failed to mention that I recently added autotest to my project and it seems that autotest had a problem with the new migration, somehow. I killed autotest and ran “rake test” instead; all passed. I then turned autotest back on to see if the same test broke and it didn’t… all passed.

So it all seems to be working now :)

Picture__87_pragsmall
10 Sep 2010, 00:29
Andrew de Andrade (19 posts)

I just arrived at this problem and I found the wording a bit awkward. What exactly are we trying to accomplish with this task? What value are we delivering to the user with this migration?

Why would I want to copy the item price into the line item? Wouldn’t this mean that the price occurs in two different locations? What if the item’s price changes (product model)? Shouldn’t the cart use the price as defined in product instead of copying that price?

Generic-user-small
07 Mar 2011, 20:50
ubique (11 posts)

The only reference to _add_price_to_line_item.rb is in eBook version B.12 at Page 387?

So up to this point a migration has not taken place which includes any price or total orders being displayed in the Admin area.

How do we fix this Sam?

Schema.rb create_table “line_items”, :force => true do |t| t.integer “product_id” t.integer “cart_id” t.datetime “created_at” t.datetime “updated_at” t.integer “quantity”, :default => 1 t.integer “order_id” end

You must be logged in to comment