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: “[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”

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”

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: “[email protected]”, 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: “[email protected]”, 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: “[email protected]”, 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).

sqlite> select * from tickets;
1|Rajinder Y.|12c|10 Rosewood
Ave.,Toronto,ON|12|[email protected]|2010-11-20 06:36:12.924931|2010-11-20
06:36:12.924931
2|Pushpa Yadav|12d|12 Rosewood
Ave.,Toronto,ON|10|[email protected]|2010-11-20 06:37:21.894155|2010-11-20
06:37:21.894155

sqlite> select * from tickets where name like “Pushpa Yadav”;

2|Pushpa Yadav|12d|12 Rosewood
Ave.,Toronto,ON|10|[email protected]|2010-11-20 06:37:21.894155|2010-11-20
06:37:21.894155

sqlite> select * from tickets where name is “Pushpa Yadav”;

2|Pushpa Yadav|12d|12 Rosewood
Ave.,Toronto,ON|10|[email protected]|2010-11-20 06:37:21.894155|2010-11-20
06:37:21.894155

sqlite> select * from tickets where name = “Pushpa Yadav”;

2|Pushpa Yadav|12d|12 Rosewood
Ave.,Toronto,ON|10|[email protected]|2010-11-20 06:37:21.894155|2010-11-20
06:37:21.894155

sqlite> select * from tickets where name like “pushpa%”;

2|Pushpa Yadav|12d|12 Rosewood
Ave.,Toronto,ON|10|[email protected]|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)


Kind Regards,
Rajinder Y. | 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

On 20 November 2010 07:25, Rajinder Y. [email protected] wrote:

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

On Nov 20, 9:11am, Colin L. [email protected] wrote:

On 20 November 2010 07:25, Rajinder Y. [email protected] wrote:

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.

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

On 10-11-20 04:11 AM, Colin L. wrote:

=> Ticket(id: integer, name: string, seat: string, address: text, price:
Look carefully and you will see that t is an array, containing all
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 Y. | 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