Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment counter and rails "first" using postgreSQL strange behavior [duplicate]

When I increment some integer column using increment_counter and passing some record id and then try to get the first record using Model.first, this return the record id plus 1.

Something like this:

Model.increment_counter :field, id
Model.first

It returns not the

Model.find(1)

but

Model.find(id+1)

Is that some particular issue of postgreSQL?

like image 949
Vinícius Avatar asked Nov 29 '25 22:11

Vinícius


1 Answers

Model.first will use the default sorting of your database (which is not necessarily an id).

Try this instead:

Model.order("id").first
like image 94
Dylan Markow Avatar answered Dec 02 '25 15:12

Dylan Markow