Forum: Ruby on Rails How to retrieve attribute values of objects?????

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.
Prashant T. (Guest)
on 2006-03-22 10:24

 I am writing following line in my code:-
@product1=Product.find(:all, :conditions => ["id = ? ",
print @product1.inspect

So I am getting on console following output.
[#<Product:0x3cd61c8 @attributes={"demand"=>nil, "name"=>"Part2",
"quantity"=>"4", "id"=>"3", "unit_price"=>"1.000"}>]

But when I am writing "print" instead of "print
@product1.inspect" in my code for displaying just name on console I m
getting following error:-
undefined method `name' for #<Array:0x3bcb720>
Why I m getting this error? & what should I write with "print" method in
above code to get value of name attribute associated with "@product1"

Thanx in advance.
Tom M. (Guest)
on 2006-03-22 10:49
(Received via mailing list)
Product.find with :all returns an array of Products.

If you're only expecting a single row back, use:

Product.find with :first

and your code will work otherwise as-is.

Notice the [] around the .inspect output

With the existing code your could use:

@product1[0].name, or much better,

-- Tom M.
This topic is locked and can not be replied to.