Rails with Postgres Data Fails

I just moved my application from MySQL to Postgres. Previously, a query for .all returned all rows in id order. In Postgres, strings are returned. Similarly

Person.first 

used to return a record with identifier 1, now it sometimes returns another record.

If I add an order clause as follows:

 Person.order("id").first 

The request succeeds and returns the first row. Is this expected behavior?

+4
source share
1 answer

this post answers your question:

I do not think that ordering by ID is guaranteed by default, since I believe in the database as a non-standard query is returned. You can configure it this way by specifying the default area at the top of the model as follows:

 default_scope order('id ASC') 

Then calling Model.all will be equivalent to calling Model.order ('id ASC').

+4
source

All Articles