I'm using a LIKE clause in Ruby On Rails. When I try to search for records by typing "more" it doesn't return anything, but when I do with "More", then it returns the records which contains More keyword, so it seems like it behaves in a case-sensitive way.
Is it possible to make this case-insensitive?
Here is the query I am using currently:
Job.where('title LIKE ? OR duration LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
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]}%")
try something like this
def query_job(query)
job_query = "%#{query.downcase}%"
Job.where("lower(title) LIKE ? or lower(duration) LIKE ?", job_query, job_query)
end
query_job(params[:search])
Related
I'm using a LIKE clause in Ruby On Rails. When I try to search for records by typing "more" it doesn't return anything, but when I do with "More", then it returns the records which contains More keyword, so it seems like it behaves in a case-sensitive way.
Is it possible to make this case-insensitive?
Here is the query I am using currently:
Job.where('title LIKE ? OR duration LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
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]}%")
try something like this
def query_job(query)
job_query = "%#{query.downcase}%"
Job.where("lower(title) LIKE ? or lower(duration) LIKE ?", job_query, job_query)
end
query_job(params[:search])
def self.search_by(search_term)
where("LOWER (course_name) LIKE :search", search_term: "%#{search_term}%")
end
this is my code, it seems LIKE query doesn't work at all
Your code where("LOWER (course_name) LIKE :search", search_term: "%#{search_term}%") isn't working because it's expecting an argument search but you're passing search_term. So, modify it to:
where("LOWER (course_name) LIKE :search", search: "%#{search_term}%")
now you're providing the same variable which it's expecting to get in "LOWER (course_name) LIKE :search".
#venues = Venue.where('name LIKE ?', "%#{#query}%")
scope :search_by, -> (search_term){ where("LOWER(course_name) LIKE ?", "%#{search_term.to_downcase}%")}
OR
def self.search_by(search_term)
where("LOWER(course_name) LIKE ?", "%#{search_term.to_downcase}%")
end
update your query to
where("LOWER (course_name) LIKE :search_term", search_term: "%#{search_term}%")
change the :search keyword with :search_term , you used alias :search in query but specify :search_term to assign search value.
The keyword ILIKE can be used instead of LIKE to match case-insensitive strings, so you don't have to use LOWER.
Also, the reason your query is not working is that you have to change the :search_term alias with :search.
def self.search_by(search_term)
where("course_name ILIKE :search", search: "%#{search_term}%")
end
I working on an app. I trying to implant a search system. Method is simple. On home i search a query on 3 column "name" OR "nomdep" OR "nomregion". After this, I wnat to filter by params with AND operator.
If I implant search with only one column for example "NAME" that's work, i can apply filters. But if i want to implant this with more than one OR I cant apply filter. I think OR operator is the problem. But for sure i have no idea to solve this...
Can you help me ? Thanks
camping.rb
has_many :caracteristiquetests, :foreign_key => :camping_id
has_many :situations, :foreign_key => :camping_id
def self.searchi(query, handicap, animaux, television, plage, etang, lac)
return scoped unless query.present?
left_outer_joins(:caracteristiquetests, :situations).where(['nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ? AND handicap LIKE ? AND animaux LIKE ? AND television LIKE ? AND plage LIKE ? AND etang LIKE ? AND lac LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%", "%#{handicap}%", "%#{animaux}%", "%#{television}%", "%#{plage}%", "%#{etang}%", "%#{lac}%"])
end
camping_controller.rb
def resultnohome
if params[:query].blank?
redirect_to action: :index and return
else
#campings = Camping.searchi(params[:query], params[:handicap], params[:animaux], params[:television], params[:plage], params[:etang], params[:lac])
end
end
caracteristiquetest.rb
belongs_to :camping
situation.rb
belongs_to :camping
EDIT
I edited my model to add "()"
camping.rb
def self.searchi(query, handicap, animaux, television, plage, etang, lac)
return scoped unless query.present?
left_outer_joins(:caracteristiquetests, :situations).where(['(nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ?) AND handicap LIKE ? AND animaux LIKE ? AND television LIKE ? AND plage LIKE ? AND etang LIKE ? AND lac LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%", "%#{handicap}%", "%#{animaux}%", "%#{television}%", "%#{plage}%", "%#{etang}%", "%#{lac}%"])
end
Unfornutualy, I m getting unexpected results : only one result is display, the first match like "Short circuit evaluation". Any ideas why ?
Just use parentheses around the or conditions, the result of the or conditions will then be combined with the and conditions.
left_outer_joins(:caracteristiquetests, :situations).where(['(nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ?) AND handicap LIKE ? AND animaux LIKE ? AND television LIKE ? AND plage LIKE ? AND etang LIKE ? AND lac LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%", "%#{handicap}%", "%#{animaux}%", "%#{television}%", "%#{plage}%", "%#{etang}%", "%#{lac}%"])
Alternatively, you can split it up into separate where statements, which will combine to make one SQL call when you retrieve the results. This would be easier to read and maintain, and doesn't do any calls where a value wasn't provided.
result = left_outer_joins(:caracteristiquetests, :situations).where('nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%")
result = result.where('handicap LIKE ?', "%#{handicap}%") if handicap
result = result.where('animaux LIKE ?', "%#{animaux}%") if animaux
result = result.where('television LIKE ?', "%#{television}%") if television
result = result.where('plage LIKE ?', "%#{plage}%") if plage
result = result.where('etang LIKE ?', "%#{etang}%") if etange
result = result.where('lac LIKE ?', "%#{lac}%") if lac
return result
I have an issue in searching records from the PostgreSQL with particular search keyword but no record is displaying here is the code
filter_text=params[:filter_search]
#outputs = Output.where("name LIKE '%#{filter_text}%'").order("name ASC")
Try this :
filter_text=params[:filter_search]
#outputs = Output.where("name LIKE ?","%#{filter_text}%").order("name ASC")
If you use ransack gem, it will allow you to use simple methods to search. Using ransack, you will only need to do this:
#outputs = Output.search(name_cont: params[:filter_search]).result.order("name ASC")
If you are going for case insensitive search go for ILIKE
filter_text = params[:filter_search]
#outputs = Output.where("name ILIKE ?", "'%#{filter_text}%'").order("name ASC")
Instead of:
filter_text=params[:filter_search]
#outputs = Output.where("name LIKE '%#{filter_text}%'").order("name ASC")
Try the following:
filter_text=params[:filter_search]
#outputs = Output.where(["name LIKE ?", "%#{filter_text}%"]).order("name ASC")
One easy way to search is to use Ransack. Which provides you an efficient search mechanism.
I am working in an app with a basic search form with Heroku, but I can't get my sql query to work properly with PostgreSQL, even though this query worked with MySQL. By the way, I tried to paste the logs from Heroku, but it only says that when you search something it renders 500.html.
Here's my model OrdemDeServico with the search action:
def self.search(search)
if search
joins(:cliente).where("clientes.nome LIKE ? OR veiculo LIKE ? OR placa LIKE ? OR ordem_de_servicos.id = ?", "%#{search}%", "%#{search}%", "%#{search}%", "#{search}")
else
where(nil)
end
end
I just installed PostgreSQL locally, and it returned this error when searching:
`PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "Augusto"
LINE 1: ... placa LIKE '%Augusto%' OR ordem_de_servicos.id = 'Augusto')
query:
SELECT "ordem_de_servicos".* FROM "ordem_de_servicos" INNER JOIN "clientes" ON "clientes"."id" = "ordem_de_servicos"."cliente_id" WHERE (clientes.nome LIKE '%Augusto%' OR veiculo LIKE '%Augusto%' OR placa LIKE '%Augusto%' OR ordem_de_servicos.id = 'Augusto') ORDER BY prazo LIMIT 5 OFFSET 0
I finally worked it out a solution. Those who have the same problem here's my code for the model:
def self.search(search)
if search
where("id = ?", search)
joins(:cliente).where("clientes.nome ilike :q or veiculo ilike :q or placa ilike :q", q: "%#{search}%")
else
where(nil)
end
end