Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails order by id (and version?)

While trying to search with id sorting (and paginating) im getting from the log:

SELECT `audits`.* FROM `audits` ORDER BY version, id DESC LIMIT 50 OFFSET 0

I am currently using this code:

@records = Audit.order("id DESC").page(page).per(50)

The problem is that the list retrieved is not correctly sorted by id descending.

BTW i am using audited-activerecord gem for audits, does this matter?

like image 355
David Mauricio Avatar asked Jun 15 '12 19:06

David Mauricio


1 Answers

I'm guessing that your gem is setting a default scope on the Audit model that orders by version, so at the moment it is ordering in ascending order by the version column, and, only if two records are the same, is it ordering by id in descending order.

To fix this, you can prepend unscoped to your chain:

Audit.unscoped.order("id DESC").page(page).per(50)
like image 90
Nick Colgan Avatar answered Oct 05 '22 08:10

Nick Colgan