rails/rails — Issue #5103: Retrieving a Single Object
Author: ak2196
Created: 2012-02-20 21:17:56+00:00
State: open

I am using Postgres 9.1.2, Rails 3.2.1 and pg 0.12.2. Simple User model. For some bizarre reason when you do a select * from users; the first result is actually the record that was inserted second. The order for all other records is the fine. I have reproduced it a few times.

So if there are only 2 records a User.first and User.last give the same result and even with multiple records the record with user_id 2 is fetched on User.first.

Model.first results in a query without and ORDER BY clause:
SELECT "users".* FROM "users" LIMIT 1;

I don't know why I am getting this erratic behaviour but wouldn't it make sense to have first add an ORDER BY clause so for User.first generates:
SELECT "users".* FROM "users" ORDER BY "users"."id" LIMIT 1;