# :first and :all giving different first records

**URL:** <https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297>\
**Category:** Rails\
**Created:** [March 31, 2009, 12:21am UTC](https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297 "2009-03-31T00:21:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 31, 2009, 12:21am UTC](https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297/1 "2009-03-31T00:21:51Z")

</div>

I’m trying to track down a strange test error. This project has Rails  
2.1.0  
frozen in vendor/rails . On my MacBook Pro, the problem boils down to  
this:

(rdb:1) Invoice.first(:order=\>‘id desc’)

#\<Invoice id: 3, type: “Invoice”, name: “Invoice 1 for Job 1”, memo:  
nil,  
created\_at: “2007-09-03 00:00:00”, updated\_at: “2009-03-30 16:49:48”,  
est\_inv\_number: “1-03”, status: “free”, qb\_txn\_id: nil, per\_quote:  
false,  
bill\_to: nil, printed\_at: nil, emailed\_at: nil, faxed\_at: nil,  
invoiceable\_type: “Job”, invoiceable\_id: 1, qb\_sent\_at: nil, company\_id:  
850010094, itemize\_tax\_markup: false, bill\_to\_customer\_id: nil,  
qb\_class\_override: nil, proposal\_loaded: false, proposal\_date: nil,  
proposal\_re: nil, proposal\_attn: nil, proposal\_body: nil,  
proposal\_include:  
nil, proposal\_exclude: nil, proposal\_sig: nil, date: “2007-09-03”\>

(rdb:1) Invoice.find(:all, :order=\>‘id desc’).first

#\<Invoice id: 9, type: “Invoice”, name: “Estimate 1 for Job 1”, memo:  
nil,  
created\_at: “2009-03-30 16:52:48”, updated\_at: “2009-03-30 16:52:48”,  
est\_inv\_number: “1-05”, status: “free”, qb\_txn\_id: nil, per\_quote:  
false,  
bill\_to: nil, printed\_at: nil, emailed\_at: nil, faxed\_at: nil,  
invoiceable\_type: “Job”, invoiceable\_id: 1, qb\_sent\_at: nil, company\_id:  
850010094, itemize\_tax\_markup: false, bill\_to\_customer\_id: nil,  
qb\_class\_override: nil, proposal\_loaded: false, proposal\_date: nil,  
proposal\_re: nil, proposal\_attn: nil, proposal\_body: nil,  
proposal\_include:  
nil, proposal\_exclude: nil, proposal\_sig: nil, date: “2009-03-30”\>

I’m in the debugger in the middle of a test run. I have another Mac,  
running the same version of the code, checked out from the same  
repository,  
on which this works fine. The SQL queries produced for these two lines  
is  
identical on both machines:

Invoice Load (0.000516) SELECT \* FROM `est_invs` WHERE (  
(`est_invs`.`type` = ‘Invoice’ ) ) ORDER BY id desc LIMIT 1

and

Invoice Load (0.000536) SELECT \* FROM `est_invs` WHERE (  
(`est_invs`.`type` = ‘Invoice’ ) ) ORDER BY id desc

Here’s another way of looking at the same weirdness:

(rdb:1) Invoice.first.id [http://invoice.first.id/](http://invoice.first.id/)  
3  
(rdb:1) Invoice.last.id [http://invoice.last.id/](http://invoice.last.id/)  
3  
(rdb:1) Invoice.all.map(&:id)  
[3, 4, 5, 6, 9]  
(rdb:1) Invoice.all.first.id [http://invoice.all.first.id/](http://invoice.all.first.id/)  
3  
(rdb:1) Invoice.all.last.id [http://invoice.all.last.id/](http://invoice.all.last.id/)  
9

On my other Mac, running the same code, same test, etc., it looks like  
this  
(which is how it should look):

(rdb:1) Invoice.first.id [http://invoice.first.id/](http://invoice.first.id/)  
3  
(rdb:1) Invoice.last.id [http://invoice.last.id/](http://invoice.last.id/)  
9  
(rdb:1) Invoice.all.map(&:id)  
[3, 4, 5, 6, 9]  
(rdb:1) Invoice.all.first.id [http://invoice.all.first.id/](http://invoice.all.first.id/)  
3  
(rdb:1) Invoice.all.last.id [http://invoice.all.last.id/](http://invoice.all.last.id/)  
9

I can’t figure out what’s going on. I’ve been staring at this off and  
on  
for days, and I’m out of ideas. Anyone have any good ones?

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 31, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297/2 "2009-03-31T17:22:05Z")

</div>

Ryan,

I had something similar on Rails 2.2.2 recently. In my case,  
Object.first was returning nil but Object.all.first was working. I  
banged my head for a bit then decided it wasn’t important to “solve”  
it at this time as the client just needed the project done, so I left  
it as Object.all.first, which isn’t as efficient, but still works.

I’d be interested if you figured this out. It seems so arbitrary.

One other thing that I would try if I were you: can you connect  
directly to MySQL and try running the queries there by hand and see  
what they return? That may help track down whether it’s even a Rails  
issue at all. I use CocoaSQL on my mac which makes it really easy to  
copy-n-paste the SQL queries from the log/debugger and try them.

If it works fine in SQL directly, then you’ve ruled that out and can  
focus on Rails, AR, caching and the like. Try it from script/console  
as well to rule out web server (which probably isn’t related anyway,  
but it’s always good rule things out).

If it doesn’t work right in SQL, then you’ve ruled out Rails, and can  
focus on what is screwed up with MySQL.

Best of luck! Post your response when you figure it out. 🙂

-Danimal

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 31, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297/3 "2009-03-31T17:40:52Z")

</div>

On Mar 31, 4:20 pm, Danimal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If it doesn’t work right in SQL, then you’ve ruled out Rails, and can  
> focus on what is screwed up with MySQL.

It’s a mysql bug: see [http://bugs.mysql.com/bug.php?id=37830](http://bugs.mysql.com/bug.php?id=37830) and  
[http://lists.mysql.com/commits/49739](http://lists.mysql.com/commits/49739)

Fred

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 31, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/first-and-all-giving-different-first-records/164297/4 "2009-03-31T19:07:43Z")

</div>

Thank you! That was it. You’re the man! 🙂

I went ahead and jumped from MySQL 5.0.x to 5.1.32, so no doubt I will  
soon  
have new adventures to relate.

On Tue, Mar 31, 2009 at 10:39 AM, Frederick C. \<
