Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NULLS FIRST / LAST for MySQL #1373

Open
stereobooster opened this issue Nov 11, 2022 · 1 comment
Open

NULLS FIRST / LAST for MySQL #1373

stereobooster opened this issue Nov 11, 2022 · 1 comment

Comments

@stereobooster
Copy link
Contributor

stereobooster commented Nov 11, 2022

For example nulls_always_last in MySQL can be simulated like this

Option 1

scope :sort_by_priority_null_asc, -> { order(Arel.sql('priority DESC')) }
scope :sort_by_priority_null_desc, -> { order(Arel.sql('-priority DESC')) }

Just an idea. There is undocumented feature for MySQL https://troels.arvin.dk/db/rdbms/#select-order_by. We can use it to implement similar to PostgreSQL behavior. I know this is not the best practice to depend on undocumented feature... just an idea

Option 2

scope :sort_by_priority_null_asc, -> { order(Arel.sql('priority IS NULL ASC, priority ASC')) }
scope :sort_by_priority_null_desc, -> { order(Arel.sql('priority IS NULL ASC, priority DESC')) }

Option 3

rails/rails#42245

@tttffff
Copy link

tttffff commented Nov 16, 2023

Hi @stereobooster, I took a bit of a dive into this recently.

I've raised a PR with Rails that implements your suggestion in Option 2 in Arel. The PR is linked on the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants