Prashant Tiwari
2006-Mar-22 08:24 UTC
[Rails] How to retrieve attribute values of objects?????
Hi, I am writing following line in my code:- ================@product1=Product.find(:all, :conditions => ["id = ? ", params[:product][''product_id''+count.to_s]]) 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 @product1.name" 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" object? Thanx in advance. Prash -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-Mar-22 08:49 UTC
[Rails] How to retrieve attribute values of objects?????
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, @product1.first.name -- -- Tom Mornini On Mar 22, 2006, at 12:24 AM, Prashant Tiwari wrote:> Hi, > > I am writing following line in my code:- > ================> @product1=Product.find(:all, :conditions => ["id = ? ", > params[:product][''product_id''+count.to_s]]) > 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 @product1.name" 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" > object? > > Thanx in advance. > Prash > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Maybe Matching Threads
- How to update values in database in this form?
- pls help me regarding Maths round up function.....
- Can anybody tell me step by step how validate data on form?
- How to check for presence of particular value entered ?
- How to get difference between two dates in days???