Find not working


#1

Hello all.

I have the following code (taken from the depot tutorial then modified)

def add_dish( dish, type, qnty )
item = @items.find {|i| i.dish_id == dish.id && i.type == type }
if item
item.quantity += qnty.to_i
else
if type == ‘Fresh’
item = Fresh.for_dish(dish)
else
item = Frozen.for_dish(dish)
end
item.order_id = 1
item.quantity = qnty.to_i
@items << item
end
@total_price += (dish.price * qnty.to_i)
end

The problem line is this:

item = @items.find {|i| i.dish_id == dish.id && i.type == type }

It appears my simple addition has messed it up (Go me!). Simply put the
above never finds a match even when one is there. I am sure it is a
simple syntax problem but I am not sure what.

Could anyone answer the obvious and put me out of my misery :slight_smile:

Thanks

Jeff


#2

On Tuesday, February 28, 2006, at 12:33 AM, Jeff J. wrote:

   item = Fresh.for_dish(dish)

Thanks

Jeff


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Jeff,

Just a guess… If this is a model object and “items” is defined via
“has_many” then you need to use “self.items” instead “@items”.

Roustem


#3

Jeff,

Just a guess… If this is a model object and “items” is defined via
“has_many” then you need to use “self.items” instead “@items”.

Roustem

@items is just an array setup like in the depot tutorial. There is no
model behind it.

class Cart
attr_reader :items
attr_reader :total_price

def initialize
empty!
end

def empty!
@items = []
@total_price = 0.0
end

Jeff