Case Insensitive Search in Rails

Case-insensitive search in Rails model

You'll probably have to be more verbose here

name = "Blue Jeans"
model = Product.where('lower(name) = ?', name.downcase).first
model ||= Product.create(:name => name)

How to make LIKE clause case-insensitive?

I assume you're using Postgres.

You can use ILIKE

Job.where('title ILIKE ? OR duration ILIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")

Or a some tricky hack lower():

Job.where('lower(title) LIKE lower(?) OR lower(duration) LIKE lower(?)', "%#{params[:search]}%", "%#{params[:search]}%")

Case insensitive search in Rails

Yes, Rails supports case-insensitive queries via the built-in Arel library or a gem such as Squeel.

How to make search box case-insensitive in rails?

You could do something like:

star = Star.where("UPPER(who) = ?", search.upcase).take

or

star = Star.where("LOWER(who) = ?", search.downcase).take

Either way, this coerces both your search term as well as the who value in the database before comparing them, which should get you the results that you need

Case insensitive search using IN operator in Ruby on Rails and PostgreSQL

This is what worked for me:

records = records.where('"' + filter['fieldName'] + '"' +
" ILIKE ANY ( array[?] )", filter['value'].map {|value| "%#{value}%" })

Case-sensitive database search with Active Record

In short: there is NO ActiveRecord idiom for case-sensitive
search.



For case-insensitive search you can try to use
this.
It still works, but source code was changed a bit. So, use it on your
own risk.

In general, case sensitivity is subset of the Collation idiom.
And different DBMS use very different default collations for string(text) data types, including default case sensitivity.
Detailed description for MySQL.

There is a sql operator COLLATE which is very common across DBMS(but seems still is not in SQL Standard).
But ActiveRecord sources show it only in schema creation code.
Neither ActiveRecord, nor Arel gems do not use COLLATE in where search(sad).

Note: Please, don't miss the database tag(mysql etc) in a Question.
There are many Questions and Answers on SO without the tags(or with sql one), which are completely irrelevant for the most of DBMSs except author's one.

Rails exists? Case insensitive

All you need to do is this:

Model.exists?(["lower(email) = ?", params[:email].downcase])

It's looking for a single argument but you're providing two. Using the array form and the find-style conditional should get what you need.

Rails Active Record case insensitive find

One of these should be good:

def self.find_by_lower_email(email)
User.find_by_email(email.downcase)
end
# OR
def self.find_by_email(email)
User.find(:all, :conditions => ["email = lower(?)", email])
end

Update:

The find_by_... functions are actually non-existing ones. When you call one of them, the ActiveRecord::Base catches your call in the method_missing method, and if the name is well formatted (contains column names, etc) then the appropriate find method is created for the class. After that it will exist.

If you have a find_by_email, it will be called by default. If it calls super, that means that you try to call ActiveRecord::Base.find_by_email, which does not exists. The missing_method catches it, and creates (actually overwrites your) find_by_email implementation. That is why not good to call super there. If you use your own implementation for finding by email, like I wrote in the second part, then it will work splendidly.

Case-insensitive search within discourse console

Add a validation in model

validates :title, presence: true, uniqueness: {case_sensitive: false}

Query

title = 'lowercase and More'
result = Topic.where('lower(title) = ?', title.downcase).first


Related Topics



Leave a reply



Submit