user568278
user568278

Reputation:

Rails 3 - Case Insensitive Query with Ransack & Postgres

My Rails app uses the Ransack gem to search my model on multiple attributes. Is there a way to have Ransack make case insensitive queries to Postgres?

= search_form_for @q do |f|
  = f.label :name_cont
  = f.text_field :name_cont

  = f.label :code
  = f.text_field :code_eq

= f.submit

Upvotes: 1

Views: 2301

Answers (2)

user568278
user568278

Reputation:

Posgres uses ILIKE for case insensitive queries.

From spovich on github:

The LIKE vs ILIKE comes from ARel, not ransack. However, that doesn't look like your issue.

Your use of :code_eq will generate SQL for an exact match (e.g. 'where code = "foo") thus cannot be case insensitive. Any of the 'like' predicates (_cont, _start, _end) generate ILIKE by default (for postgresql). So if you use :code_cont that will generate an ILIKE statement (e.g. 'where code ILIKE "%foo%"'). See the constants for all the predicates.

https://github.com/ernie/ransack/blob/master/lib/ransack/constants.rb

https://github.com/ernie/ransack/issues/57#issuecomment-3472618

Upvotes: 2

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

Consider using the matches_any predicate. I haven't seen any docs or specs on it in the github repo, but this issue discusses it, https://github.com/ernie/ransack/issues/11

Upvotes: 0

Related Questions