activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.68k stars 807 forks source link

Default sort :nulls_last and :nulls_first are reversed #1399

Open scarroll32 opened 1 year ago

scarroll32 commented 1 year ago

In this MR default sort for NULLS FIRST or NULLS LAST was added, but the order seems to be reversed.

When adding to config/initilizers/ransack.rb:

Ransack.configure do |c|
  c.postgres_fields_sort_option = :nulls_first 
end

It produces this SQL:

10:24:14 web.1    |   User Load (0.7ms)  SELECT "users".* FROM "users" ORDER BY "users"."last_sign_in_at" DESC NULLS LAST LIMIT $1 OFFSET $2  [["LIMIT", 50], ["OFFSET", 0]]

Then when the setting is reversed:

Ransack.configure do |c|
  c.postgres_fields_sort_option = :nulls_last
end

This is the SQL:

10:29:02 web.1    |   User Load (0.9ms)  SELECT "users".* FROM "users" ORDER BY "users"."last_sign_in_at" DESC NULLS FIRST LIMIT $1 OFFSET $2  [["LIMIT", 50], ["OFFSET", 0]]
scarroll32 commented 1 year ago

@itsalongstory is there a bug here or am I missing something? 🤔

itsalongstory commented 1 year ago

@itsalongstory is there a bug here or am I missing something? 🤔

It's not a bug, configuration is setting for ASC

ASC NULLS FIRST = DESC NULLS LAST

itsalongstory commented 1 year ago

https://github.com/activerecord-hackery/ransack/blob/0c5078c462716c73ac337241235b03718a478cca/lib/ransack/adapters/active_record/context.rb#L45-L54

You may need set it to :nulls_always_first or :nulls_always_last