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 general, case sensitivity is subset of the Collation idiom.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.
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}
Querytitle = 'lowercase and More'
result = Topic.where('lower(title) = ?', title.downcase).first
Related Topics
How to Avoid Circular Creation of Associated Models in Factory_Girl
Clean Install Osx 10.9.1 Returns "Undefined Method 'Path2Class'" When Trying to Install Gems
Recommended Way to Generate a Presigned Url to S3 Bucket in Ruby
Functional Testing of a Restful Post in Ruby on Rails
Rails/Postgres, 'Foreign Keys' Stored in Array to Create 1-Many Association
How to Reference a Local Gem from a Ruby Script
Activemodel::Validations on Anonymous Class
How to Handle Omniauth Callbacks in Multiple Environments
How to Properly Manage The Rails Tmp Directory
Using Update_Columns in Rails 3
Puma and Nginx 502 Bad Gateway Error (Ubuntu Server 14.04)
How to Handle Single Table Inheritance in Simpleform So a Single Helper Handles All Models
Rails and JavaScript Minifier and Aggregator