views:

54

answers:

3

cart.rb:

def vendor3name
       @items.first { |item| item.vender.name }
end

When I call the method, I'm looking for the vendor name but it returns the vendor ID#. What am I doing wrong?

<%= @cart.vendor3name %>

CartItem:0x264c358

+1  A: 

try

@items.first.vendor.name
Ben
+2  A: 

If you want the name of the first vendor of the collection, I think this is the code you must execute:

@items.first.vender.name

using first with a block seems to be returning the first id that matches the expression in the block given or something like that.

Pablo Fernandez
A: 
@items.first.vender.name

is the correct way to do this, what you are seeing is not the ID, it's showing you the object. When you attempt to print out an object in Rails it prints out the object type and the object's address in memory separated with a colon.

Using the block that you are doesn't return anything, it's simply doing what you have within the {} to the object, so you are calling the .name method but the block doesnt return anything.

@items.first.vender.name returns the first vender's name

Rabbott