Ruby on Rails order by id (and version?)

When trying to search with sorting id (and pagination) im obtained 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 extracted list is not sorted in descending order by name.

By the way, I use audit-activerecord auditing for audits, doesn't it matter?

+7
source share
3 answers

I assume that your gem sets the default scope for the audit model, which orders by version, so at the moment it orders in ascending order by the version column and, only if the two records are the same, it orders by id in descending order.

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

 Audit.unscoped.order("id DESC").page(page).per(50) 
+12
source

to try:

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

except('order') should remove any order relationships added to the request

+7
source
 Audit.descending.page(page).per(50) 

source link

0
source

All Articles