[ActiveRecord] Add NULLS LAST ordering to Arel

Hi team!

We’re currently using Arel to dynamically construct complex SQL queries that power sorting and pagination in our API. One of the features we’d like to make use of is the NULLS LAST modifier in ORDER BY clauses. Arel has no support for this, but it’s a small and straightforward patch (see https://github.com/hmac/rails/commit/7cab443ab1e67536d8d4e7a49c7126c815a96ad5).

Would anyone be against adding this feature?

Further info:

  • the patch only adds support for PostgreSQL since I know it supports this feature - I’m happy to add it to other visitors if we can confirm their support.

  • similarly, we can easily add NULLS FIRST support as well (we don’t need it ourselves so it’s not included here).

Thanks!

I just use a raw sql string for NULLS LAST.

Hi Andrew,

So far we’ve managed to entirely avoid using SQL literals for this component, instead relying on (and trusting) Arel. If possible I’d like to keep that constraint, because it has a number of safety advantages (namely, discouraging the use of SQL literals). Arel nodes also arguably have better ergonomics, in that we can fold them together using #and and #or and be fairly confident that we’ll get valid SQL out at the end.

1 Like

This is now in Arel, as .order(User.arel_table[:first_name].desc.nulls_last) :tada: