Case-insensitive search in Rails model Case-insensitive search in Rails model ruby-on-rails ruby-on-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)


This is a complete setup in Rails, for my own reference. I'm happy if it helps you too.

the query:

Product.where("lower(name) = ?", name.downcase).first

the validator:

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

the index (answer from Case-insensitive unique index in Rails/ActiveRecord?):

execute "CREATE UNIQUE INDEX index_products_on_lower_name ON products USING btree (lower(name));"

I wish there was a more beautiful way to do the first and the last, but then again, Rails and ActiveRecord is open source, we shouldn't complain - we can implement it ourselves and send pull request.


If you are using Postegres and Rails 4+, then you have the option of using column type CITEXT, which will allow case insensitive queries without having to write out the query logic.

The migration:

def change  enable_extension :citext  change_column :products, :name, :citext  add_index :products, :name, unique: true # If you want to index the product namesend

And to test it out you should expect the following:

Product.create! name: 'jOgGers'=> #<Product id: 1, name: "jOgGers">Product.find_by(name: 'joggers')=> #<Product id: 1, name: "jOgGers">Product.find_by(name: 'JOGGERS')=> #<Product id: 1, name: "jOgGers">