Forum: Ruby on Rails Odd problem in Agile tutorial

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
James B. (Guest)
on 2006-02-27 18:30
I am getting an undefined method error in my cart.rb class when trying
to find in an array.  I cannot see what my error is.  Perhaps a view
more eyes can help me with this:

      1 class Cart
      3   attr_reader :items
      4   attr_reader :total_price
      6   def initialize
      7     @items = []
      8     @total_price = 0.0
      9   end
     11   def add_product(product)
     12     #   @items << LineItem.for_product(product)
     13     item = @items.find {|i| i.product_id ==}
     14     if item
     15       item.quantity += 1
     16     else
     17       item = LineItem.for_product(product)
     18       @items << item
     19     end
     20     @total_price += product.price
     21   end
     23 end


gives this in the stack trace:

#{RAILS_ROOT}/app/models/cart.rb:13:in `add_product'

The only method that I can see called in line 13 is #find since
product_id is a defined column in line_items:

CREATE TABLE line_items (
    id bigserial NOT NULL,
    product_id bigint NOT NULL,
    quantity numeric(10,2) DEFAULT 0 NOT NULL,
    unit_price money NOT NULL

with this as line_items.rb in app/models

      1 class LineItem < ActiveRecord::Base
      3   belongs_to :product
      5   def self.for_product(product)
      6     item =
      7     item.quantity = 1
      8     item.product = product
      9     item.unit_price = product.price
     10     item
     11   end
     12 end

Can anyone provide me with a clue about what I have missed?

James B. (Guest)
on 2006-02-27 19:17
I resolved this by first stopping the webrick server and then flushing
the browser cache and cookies.  I am not sure why this worked but it
must have since I made no changes to the code.  If someone can enlighten
me regarding this behaviour I would apprciated it.  The browser in use
is Firefox v1.5.0.1.

This topic is locked and can not be replied to.