On 10-11-20 04:11 AM, Colin Law wrote:
On 20 November 2010 07:25, Rajinder Yadav<devguy...@gmail.com>  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:
"ya...@email.ca", 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:
"ya...@email.ca", 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


Thanks Colin, that makes sense! I had it in my head I was only getting back a single row, failed to see an array was being returned.

--
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

--
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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to