Rails 3 Active Record bug?

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.

yadav@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: "yadav@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"

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: "yadav@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"

irb(main):007:0* t = Ticket.where "name is ?", "Pushpa Yadav" => [#<Ticket id: 2, name: "Pushpa Yadav", seat: "12d", address: "12 Rosewood Ave.,Toronto,ON", price: #<BigDecimal:33d1078,'0.1E2',9(18)>, email: "yadav@email.ca", created_at: "2010-11-20 06:37:21", updated_at: "2010-11-20 06:37:21">] irb(main):008:0> t.name

=> "Ticket"

irb(main):009:0> t = Ticket.where "name = ?", "Pushpa Yadav" => [#<Ticket id: 2, name: "Pushpa Yadav", seat: "12d", address: "12 Rosewood Ave.,Toronto,ON", price: #<BigDecimal:33bcbc8,'0.1E2',9(18)>, email: "yadav@email.ca", created_at: "2010-11-20 06:37:21", updated_at: "2010-11-20 06:37:21">] irb(main):010:0> t.name => "Ticket"

irb(main):010:0> t = Ticket.where "name like ?", "pushpa%" => [#<Ticket id: 2, name: "Pushpa Yadav", seat: "12d", address: "12 Rosewood Ave.,Toronto,ON", price: #<BigDecimal:33bcbc8,'0.1E2',9(18)>, email: "yadav@email.ca", created_at: "2010-11-20 06:37:21", updated_at: "2010-11-20 06:37:21">] irb(main):010:0> t.name => "Ticket"

I am using sqlite3, here is the sqlite3 console output (which seems to work fine).

select * from tickets;

1|Rajinder Yadav|12c|10 Rosewood Ave.,Toronto,ON|12|yadav@email.ca|2010-11-20 06:36:12.924931|2010-11-20 06:36:12.924931 2|Pushpa Yadav|12d|12 Rosewood Ave.,Toronto,ON|10|yadav@email.ca|2010-11-20 06:37:21.894155|2010-11-20 06:37:21.894155

select * from tickets where name like "Pushpa Yadav";

2|Pushpa Yadav|12d|12 Rosewood Ave.,Toronto,ON|10|yadav@email.ca|2010-11-20 06:37:21.894155|2010-11-20 06:37:21.894155

select * from tickets where name is "Pushpa Yadav";

2|Pushpa Yadav|12d|12 Rosewood Ave.,Toronto,ON|10|yadav@email.ca|2010-11-20 06:37:21.894155|2010-11-20 06:37:21.894155

select * from tickets where name = "Pushpa Yadav";

2|Pushpa Yadav|12d|12 Rosewood Ave.,Toronto,ON|10|yadav@email.ca|2010-11-20 06:37:21.894155|2010-11-20 06:37:21.894155

select * from tickets where name like "pushpa%";

2|Pushpa Yadav|12d|12 Rosewood Ave.,Toronto,ON|10|yadav@email.ca|2010-11-20 06:37:21.894155|2010-11-20 06:37:21.894155

my gem list is yadav@Six9:$ gem list

*** LOCAL GEMS ***

abstract (1.0.0) actionmailer (3.0.1) actionpack (3.0.1) activemodel (3.0.1) activerecord (3.0.1) activeresource (3.0.1) activesupport (3.0.1) arel (2.0.1, 1.0.1) authlogic (2.1.6) autotest (4.4.2, 4.4.1) autotest-rails-pure (4.1.1, 4.1.0) babosa (0.2.0) bcrypt-ruby (2.1.2) builder (2.1.2) bundler (1.0.3) calendar_date_select (1.16.1) cancan (1.4.0) devise (1.1.3) diff-lcs (1.1.2) erubis (2.6.6) fastri (0.3.1.1) forgery (0.3.6) friendly_id (3.1.7) grackle (0.1.10) gruff (0.3.6) i18n (0.4.2) json (1.4.6) mail (2.2.9) mechanize (1.0.0) mime-types (1.16) minitest (1.7.2, 1.6.0) nifty-generators (0.4.2) nokogiri (1.4.3.1) oauth (0.4.4, 0.4.3) patron (0.4.10, 0.4.9) polyglot (0.3.1) rack (1.2.1) rack-mount (0.6.13) rack-test (0.5.6) rails (3.0.1) railties (3.0.1) rake (0.8.7) rdoc (2.5.11, 2.5.8) RedCloth (4.2.3) rmagick (2.13.1) rspec (2.0.1) rspec-core (2.0.1) rspec-expectations (2.0.1) rspec-mocks (2.0.1) rspec-rails (2.0.1) sqlite3-ruby (1.3.2, 1.3.1) thor (0.14.4, 0.14.3) tiny_mce (0.1.4) tkri (0.9.5) treetop (1.4.8) tzinfo (0.3.23) w3c_validators (1.1.1) warden (1.0.1, 0.10.7) webrat (0.7.2) will_paginate (3.0.pre2, 2.3.15) wxruby (2.0.0 x86_64-linux)

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.

yadav@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: "yadav@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: "yadav@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

Because it's not actually an array, it's a Arel scope and you can call class methods on such scopes (the method is called with conditions etc. set by the scope). In this case it's not very useful because the class method name just returns the name of the class but it still works.

fred

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.