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

Case Insensitive Sorting in Postgresql #1201

Closed
shingz96 opened this issue Jan 23, 2021 · 11 comments · Fixed by #1213
Closed

Case Insensitive Sorting in Postgresql #1201

shingz96 opened this issue Jan 23, 2021 · 11 comments · Fixed by #1213

Comments

@shingz96
Copy link

I would like to ask that is there any possibility to have a way so that can be put in configuration to make the sorting order be done in case insensitive way in Postgresql SQL?

@scarroll32
Copy link
Member

@shingz96 does *_i_cont work for you?

See https://github.com/activerecord-hackery/ransack#search-matchers

Screen Shot 2021-01-24 at 00 02 59

@shingz96
Copy link
Author

that is for searching, what I am looking for is sorting

@lucashungaro
Copy link

@shingz96 Hi! Did you find any workarounds for this? I'm thinking about monkey patching Ransack to be able to do something like ORDER BY LOWER(field_name)

@shingz96
Copy link
Author

@shingz96 Hi! Did you find any workarounds for this? I'm thinking about monkey patching Ransack to be able to do something like ORDER BY LOWER(field_name)

I did found a possible way from this stackoverflow answer, but take note that this may break query when using DISTINCT,
so i assume there is no other way but manually order by lowercase instead

https://stackoverflow.com/a/34677378

@lucashungaro
Copy link

@shingz96 Ah, right! That's along the lines of what I was considering. Thank you for replying!

@scarroll32
Copy link
Member

Some more information here about this topic https://www.postgresql.org/docs/13/citext.html

@scarroll32
Copy link
Member

scarroll32 commented Feb 24, 2021

Copying from the SO answer https://stackoverflow.com/a/34677378

You can solve this for all string columns of a model at once by using this approach:

lib/ransack_object.rb

module RansackObject

  def self.included(base)
    base.columns.each do |column|
      if column.type == :string
        base.ransacker column.name.to_sym, type: :string do
          Arel.sql("lower(#{base.table_name}.#{column.name})")
        end
      end
    end
  end

end

Then include the ransack object in your model:

class UserWithManyAttributes < ActiveRecord::Base

  include RansackObject

end

@scarroll32
Copy link
Member

@lucashungaro
Copy link

Thanks @seanfcarroll!

@sspread
Copy link

sspread commented Jan 19, 2023

This solution applies 'lower' to searching as well. Is there a way to only apply to sorting, handling relations?

@MeterSoft
Copy link

This solution applies 'lower' to searching as well. Is there a way to only apply to sorting, handling relations?

You can do something like this and control where you want to do lower search or sort by adding _ci to field name

module RansackObject

  def self.included(base)
    base.columns.each do |column|
      if column.type == :string
        base.ransacker "#{column.name}_ci".to_sym, type: :string do
          Arel.sql("lower(#{base.table_name}.#{column.name})")
        end
      end
    end
  end

end

Model.ransack(s: "name_ci asc")

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

Successfully merging a pull request may close this issue.

5 participants