Rails 3.2 - OR query not working properly - Rewriting query - ruby-on-rails

I was really happy using this query for the search form. However, just found out that is not working properly.
The failing bit is the .where which like against params[:query]. This like happens 4 times against 4 different fields. However, results shows that this is not happening.
#events_casual = Event.non_timetable.where("events.finish_date >= ?", #time).where((["CAST(headers.title_es as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(headers.title_en as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(headers.title_eu as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(headers.title_fr as varchar(255)) LIKE ?", "%#{params[:query]}%"])).includes(:header).order("events.start_date ASC")
I have been trying to transform this query into:
#events_casual = Event.non_timetable.joins(:header).where(" params[:query] in (?)", [headers.title_es, headers.title_en, headers.title_eu]).order("events.start_date ASC")
But, this does not work. Error: undefined methodtitle_es' for {}:Hash`
I have tried similar syntax. But to not avail.
Any help rewriting that query much appreciated. Thanks

try the following
#events_casual = Event
.non_timetable
.includes(:header)
.order("events.start_date ASC")
.where("events.finish_date >= ?", #time)
.where("CAST(headers.title_es as varchar(255)) LIKE :query OR CAST(headers.title_en as varchar(255)) LIKE :query OR CAST(headers.title_eu as varchar(255)) LIKE :query OR CAST(headers.title_fr as varchar(255)) LIKE :query", { query: "%#{params[:query]}%"])

Related

Rails - Where Like query is not returning results that are all capital letters [duplicate]

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])

Result of where query in rails

I am performing a db search as below:
abc = params[:search]
Model.where("column_name ilike ?", "%#{abc}%")
When I am searching nothing it is running like below:
Model.where("column_name ilike ?", "%%")
which is giving me all rows of table.
When my search param is empty I want it to run like:
Model.where("column_name ilike ?", "")
How can I achieve that?
Well, just check whether the params is an empty string:
Model.where("column_name ilike ?", abc.blank? ? '' : "%#{abc}%")
# or, more strict check
Model.where("column_name ilike ?", abc == '' ? '' : "%#{abc}%")

How to make LIKE clause case-insensitive?

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])

Postgres Rails 4 search query id or multiple columns

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

Rails 3.2 - ambiguous column name: title_es: - What is the right syntax?

I am repeatingly having the same error when trying to deal with my search form. Having a look at the internet seems clear that my query needs to be more specific so Rails knows which column to look at, at any single moment.
From the error I understand that the problem comes from the 'title_es' column which belongs to Headers. However, I have tried many different ways and I cannot get the right syntax to make it work. Any help will be much appreciated! Thanks
Event Model
scope :with_timetable, joins(:header).where(headers: {weekly: true})
Event controller
def search
#events_weekly = Event.with_timetable.where((["CAST(title_es as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(title_en as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(title_eu as varchar(255)) LIKE ?", "%#{params[:query]}%"] || ["CAST(title_fr as varchar(255)) LIKE ?", "%#{params[:query]}%"])).includes(:header)
if #time.wday == 1
#events = #events_weekly.joins(:timetable).where(timetables: {mon: true})
elsif #time.wday == 2
.........
end
You should specify table name, something like "CAST(events.title_es as varchar(255))" or "CAST(headers.title_es as varchar(255))" depending on what you need.
I guess you have to specify table name title_eu too.

Resources