Skip to content

Commit

Permalink
Merge pull request #1213 from activerecord-hackery/fix-1201
Browse files Browse the repository at this point in the history
How to do case insensitive searches
  • Loading branch information
scarroll32 authored Feb 24, 2021
2 parents 5d667b5 + 065d350 commit 301dadf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ end

See this feature: https://www.postgresql.org/docs/13/queries-order.html

#### Case Insensitive Sorting in PostgreSQL

In order to request PostgresSQL to do a case insensitive sort for all string columns of a model at once, Ransack can be extended by using this approach:

```ruby
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
```

```ruby
class UserWithManyAttributes < ActiveRecord::Base
include RansackObject
end
```

If this approach is taken, it is advisible to [add a functional index](https://www.postgresql.org/docs/13/citext.html).

This was originally asked in [a Ransack issue](https://github.com/activerecord-hackery/ransack/issues/1201) and a solution was found on [Stack Overflow](https://stackoverflow.com/a/34677378).

### Advanced Mode

"Advanced" searches (ab)use Rails' nested attributes functionality in order to
Expand Down

0 comments on commit 301dadf

Please sign in to comment.