简体   繁体   中英

Rails ordering with nil last - SQLite3 error

Currently, I am trying to order my class by the limit attribute. I want the values to be in ascending order, but with all nil values last.

I've tried a couple different queries ActiveRecord query along with a cross base SQL query (it needs to be SQL flexible). I've tried a few:

self.order('ISNULL(limit), limit ASC')
self.order('CASE WHEN -limit DESC')
self.order('limit IS NULL, limit DESC')

But I keep getting errors around limit , am I missing something?

SQLite3::SQLException: near "ISNULL": syntax error: SELECT "table".*      
FROM "table"  WHERE "table"."deleted_at" IS NULL ORDER BY ISNULL(limit),   
limit ASC

And for anyone curious, I used self.order("-cutoff DESC") . It's simple and gets the job done.

limit IS NULL可以做到,但是limit是sqlite3中的保留键,因此请将其括在反引号或引号中:

self.order('`limit` IS NULL')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM