On 20 November 2010 07:25, Rajinder Yadav <[email protected]> wrote:
> I'm getting strange results with rails 3.0.1
>
> In each case the query seems to return the correct row, however only one of
> them gives me the correct value for the name field.
>
> ya...@six9:$ rails console
> Loading development environment (Rails 3.0.1)
>
> irb(main):001:0> Ticket
> => Ticket(id: integer, name: string, seat: string, address: text, price:
> decimal, email: string, created_at: datetime, updated_at: datetime)
>
> irb(main):002:0> t = Ticket.where "name like ?", "Pushpa Yadav"
> => [#<Ticket id: 2, name: "Pushpa Yadav", seat: "12d", address: "12 Rosewood
> Ave.,Toronto,ON", price: #<BigDecimal:34cd558,'0.1E2',9(18)>, email:
> "[email protected]", created_at: "2010-11-20 06:37:21", updated_at: "2010-11-20
> 06:37:21">]
> irb(main):003:0> t.name
> => "Ticket"

Look carefully and you will see that t is an array, containing all
rows that match, though only one in this case.  so you must do
t[0].name or t.first.name or similar.  I am not sure why t.name
evaluates to "Ticket" though.

>
> irb(main):004:0> t = Ticket.find 2
> => #<Ticket id: 2, name: "Pushpa Yadav", seat: "12d", address: "12 Rosewood
> Ave.,Toronto,ON", price: #<BigDecimal:3443dd0,'0.1E2',9(18)>, email:
> "[email protected]", created_at: "2010-11-20 06:37:21", updated_at: "2010-11-20
> 06:37:21">
> irb(main):005:0> t.name
> => "Pushpa Yadav"

As you have asked for a single row this time it is not an array.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to