Ruby on Rails iterate array result into query - ruby-on-rails

Following were my output from the query:
unless #slug == 'all'
#city = '' if #city.blank?
#city = #city.gsub("-", " ")
country = Country.where("lower(name) LIKE ?", "#{#city.downcase}")
gon.country = country
if country.present?
end
if #city.present?
#products = #products.where("lower(city) LIKE ? or lower(country) like ? or lower(state) LIKE ?", "%#{#city.downcase}%", "%#{#city.downcase}%","%#{#city.downcase}%")
#city_obj = City.where("lower(name) LIKE ?", "%#{#city.downcase}%").first
end
end
Here gon.country return result as:
Object
countries
:
Array[2]
0
:
"california"
1
:
"san francisco"
How can I iterate the countries and pass it to get #products result?

You can do it in two ways :
Iterate all country record and then add like query.
#product = []
country.each do |con|
#products << #products.where("lower(city) LIKE ? or lower(country) like ? or lower(state) LIKE ?", "%#{con.downcase}%", "%#{con.downcase}%","%#{con.downcase}%")
end
You can use ILIKE syntax of core sql. Which work like this :
select * from table where value ilike any (array['%foo%', '%bar%', '%baz%']);
# in your case
country_array_with_percent = country.map {|con| "%#{con}%" }
#products.where("name ILIKE ANY ( array[?] )", country_array_with_percent)
You can also make query chaining with or query, More details here

Related

How to select all records that start with certain string?

I have several records for which title starts with:
Nick1
Nick2
Nick3
Othername1
How can I select all the records of which the title starts with "Nick" and have them rendered in the correct order? Something like:
#records = Record.where(title starts with: params[:title_name])
render json: #records
You can use the LIKE operator here:
#records = Record.where('title LIKE ?', "#{params[:title_name]}%").order(:title)
I would prefer to put these into a scope:
scope :title_search, ->(title){ where('title LIKE ?', "#{title}%") }
and call it via:
#records = Record.title_search(params[:title_name])
You can try Searchlogic .
Then it's as easy as:
#search = Record.new_search(params[:search])
#search.condition.title_starts_with = "Nick"
#models = #search.all
Or you can try
Record.where("title LIKE :prefix", prefix: "#{prefix}%")
I'd recommend using arel as in "How to do a LIKE query in Arel and Rails?"
records = Record.arel_table
Record.where records[:title].matches("#{params[:title_name]}%")

Rails Search ActiveRecord with Logical Operators

I'm wondering what the best way to parse a text query in Rails is, to allow the user to include logical operators?
I'd like the user to be able to enter either of these, or some equivalent:
# searching partial text in emails, just for example
# query A
"jon AND gmail" #=> ["jonsmith#gmail.com"]
# query B
"jon OR gmail" #=> ["jonsmith#gmail.com", "sarahcalaway#gmail.com"]
# query C
"jon AND gmail AND smith" #=> ["jonsmith#gmail.com"]
Ideally, we could get even more complex with parentheses to indicate order of operations, but that's not a requirement.
Is there a gem or a pattern that supports this?
This is a possible but inefficient way to do this:
user_input = "jon myers AND gmail AND smith OR goldberg OR MOORE"
terms = user_input.split(/(.+?)((?: and | or ))/i).reject(&:empty?)
# => ["jon myers", " AND ", "gmail", " AND ", "smith", " OR ", "goldberg", " OR ", "MOORE"]
pairs = terms.each_slice(2).map { |text, op| ["column LIKE ? #{op} ", "%#{text}%"] }
# => [["column LIKE ? AND ", "%jon myers%"], ["column LIKE ? AND ", "%gmail%"], ["column LIKE ? OR ", "%smith%"], ["column LIKE ? OR ", "%goldberg%"], ["column LIKE ? ", "%MOORE%"]]
query = pairs.reduce([""]) { |acc, terms| acc[0] += terms[0]; acc << terms[1] }
# => ["column LIKE ? AND column LIKE ? AND column LIKE ? OR column LIKE ? OR column LIKE ? ", "%jon myers%", "%gmail%", "%smith%", "%goldberg%", "%MOORE%"]
Model.where(query[0], *query[1..-1]).to_sql
# => SELECT "courses".* FROM "courses" WHERE (column LIKE '%jon myers%' AND column LIKE '%gmail%' AND column LIKE '%smith%' OR column LIKE '%goldberg%' OR column LIKE '%MOORE%' )
However, as I said, searches like this one are extremely inefficient. I'd recommend you use a full-text search engine, like Elasticsearch.
I use such a parser in a Sinatra app, since the queries tend to be complex I produce plain SQL instead of using the activerecords selection methods.
If you can use it, feel free..
You use it like this, class_name is the activerecord class representing the table, params is a hash of strings to parse, the result is sent to the browser as Json
eg
generic_data_getter (Person, {age: ">30",name: "=John", date: ">=1/1/2014 <1/1/2015"})
def generic_data_getter (class_name, params, start=0, limit=300, sort='id', dir='ASC')
selection = build_selection(class_name, params)
data = class_name.where(selection).offset(start).limit(limit).order("#{sort} #{dir}")
{:success => true, :totalCount => data.except(:offset, :limit, :order).count, :result => data.as_json}
end
def build_selection class_name, params
field_names = class_name.column_names
selection = []
params.each do |k,v|
if field_names.include? k
type_of_field = class_name.columns_hash[k].type.to_s
case
when (['leeg','empty','nil','null'].include? v.downcase) then selection << "#{k} is null"
when (['niet leeg','not empty','!nil','not null'].include? v.downcase) then selection << "#{k} is not null"
when type_of_field == 'string' then
selection << string_selector(k, v)
when type_of_field == 'integer' then
selection << integer_selector(k, v)
when type_of_field == 'date' then
selection << date_selector(k, v)
end
end
end
selection.join(' and ')
end
def string_selector(k, v)
case
when v[/\|/]
v.scan(/([^\|]+)(\|)([^\|]+)/).map {|p| "lower(#{k}) LIKE '%#{p.first.downcase}%' or lower(#{k}) LIKE '%#{p.last.downcase}%'"}
when v[/[<>=]/]
v.scan(/(<=?|>=?|=)([^<>=]+)/).map { |part| "#{k} #{part.first} '#{part.last.strip}'"}
else
"lower(#{k}) LIKE '%#{v.downcase}%'"
end
end
def integer_selector(k, v)
case
when v[/\||,/]
v.scan(/([^\|]+)([\|,])([^\|]+)/).map {|p|p p; "#{k} IN (#{p.first}, #{p.last})"}
when v[/\-/]
v.scan(/([^-]+)([\-])([^-]+)/).map {|p|p p; "#{k} BETWEEN #{p.first} and #{p.last}"}
when v[/[<>=]/]
v.scan(/(<=?|>=?|=)([^<>=]+)/).map { |part| p part; "#{k} #{part.first} #{part.last}"}
else
"#{k} = #{v}"
end
end
def date_selector(k, v)
eurodate = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{1,4})$/
case
when v[/\|/]
v.scan(/([^\|]+)([\|])([^\|]+)/).map {|p|p p; "#{k} IN (DATE('#{p.first.gsub(eurodate,'\3-\2-\1')}'), DATE('#{p.last.gsub(eurodate,'\3-\2-\1')}'))"}
when v[/\-/]
v.scan(/([^-]+)([\-])([^-]+)/).map {|p|p p; "#{k} BETWEEN DATE('#{p.first.gsub(eurodate,'\3-\2-\1')}')' and DATE('#{p.last.gsub(eurodate,'\3-\2-\1')}')"}
when v[/<|>|=/]
parts = v.scan(/(<=?|>=?|=)(\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4})/)
selection = parts.map do |part|
operator = part.first ||= "="
date = Date.parse(part.last.gsub(eurodate,'\3-\2-\1'))
"#{k} #{operator} DATE('#{date}')"
end
when v[/^(\d{1,2})[-\/](\d{1,4})$/]
"#{k} >= DATE('#{$2}-#{$1}-01') and #{k} <= DATE('#{$2}-#{$1}-31')"
else
date = Date.parse(v.gsub(eurodate,'\3-\2-\1'))
"#{k} = DATE('#{date}')"
end
end
The simplest case would be extract an array from the strings:
and_array = "jon AND gmail".split("AND").map{|e| e.strip}
# ["jon", "gmail"]
or_array = "jon OR sarah".split("OR").map{|e| e.strip}
# ["jon", "sarah"]
Then you could construct an query string:
query_string = ""
and_array.each {|e| query_string += "%e%"}
# "%jon%%gmail%"
Then you use a ilike or a like query to fetch the results:
Model.where("column ILIKE ?", query_string)
# SELECT * FROM model WHERE column ILIKE '%jon%%gmail%'
# Results: jonsmith#gmail.com
Of course that could be a little overkill. But it is a simple solution.

Rails Activerecord Ambiguous column name on .joins

Here is my code. It work fine if I have something in the :search field or if I have something in the :supplier field but if I have something in both i get "Ambiguous column name 'NUMBER'". Is there a way to select AS or something?
#date_from = params[:date_from] || Date.today.beginning_of_month.strftime('%m/%d/%Y')
#date_to = params[:date_to] || Date.today.strftime('%m/%d/%Y')
q = "%#{params[:search]}%"
#products = Product.where("DISCONT = ? AND NONPRODUCT = ?" ,0,0)
#products = #products.where('NUMBER like ?' ,q) if params[:search].present?
#products = #products.joins(:supplier_items).where('SUPPLIER = ?' ,params[:supplier]) if params[:supplier].present?
#products = #products.paginate(page: params[:page], per_page: 25)
Just prefix the number with the table name
For example:
#products = Product.where(:discount => 0, :nonproduct => 0)
#products = #products.where('products.number like ?', query)
I'm guessing your suppliers table and products table both have a column named "number". Honestly, you'd be best off running a migration to change the column names now (maybe supplier_num / product_num) because keeping it something as generic as "number" will likely keep causing you headaches.

Ruby on Rails: Organize big search method

I have big search method in my model that corresponds to search proper Car.
It looks like this:
def self.search(params)
cars = joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?",
params[:return_date], params[:handover_date])
cars = joins(:car_class).where("car_classes.id= ?", params[:car_class])
cars = cars_at_both_locations(params[:handover_location], params[:return_location])
cars = params[:car_body_style] == [""] ? cars : joins(:car_configuration).
where("car_configurations.body_style_id = ?", params[:car_body_style])
cars = params[:car_fuel] == [""] ? cars : where(fuel: params[:car_fuel])
cars = params[:car_transmission] == [""] ? cars : where(transmission: params[:car_transmission])
cars = params [:car_seats] == [""] ? cars : car_seats(params[:car_seats])
cars = Car.joins(:prices).where('prices.to_days >= ?',
(Date.parse(params[:return_date]) - Date.parse(params[:handover_date])).to_i)
end
It is very unreadable...
Did anyone have any idea to refactor this method to be more readable?
You should use scopes with proper names for each method in your Car model
for example for your first two lines, add in your model
scope :available, ->(handover_date,return_date) { joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?",
return_date, handover_date)}
scope :with_class, ->(car_class_id) {where("car_classes.id= ?", car_class_id)}
You will then just have to write
def self.search(opts)
available(opts[:handover_date],opts[:return_date]).
with_class(opts[:car_class_id]).
[...]
end
You will also be able to use the scopes available and with class everywhere, which is pretty cool too.

Better search query for two columns forename and name

Actually i have this search query from MrJoshi here is the associated question:
Search query for (name or forename) and (name forname) and (forname name)
def self.search(query)
return where('FALSE') if query.blank?
conditions = []
search_columns = [ :forname, :name ]
query.split(' ').each do |word|
search_columns.each do |column|
conditions << " lower(#{column}) LIKE lower(#{sanitize("%#{word}%")}) "
end
end
conditions = conditions.join('OR')
self.where(conditions)
end
The problem with this search query is that it returns way to much records. For example if somebody is searching for John Smith this search query returns all records wih the forename John and all records with the name Smith although there is only one person that exactly matches the search query means name is Smith and forename is John
So i changed the code a little bit:
def self.search(query)
return where('FALSE') if query.blank?
conditions = []
query2 = query.split(' ')
if query2.length == 2
conditions << " lower(:forname) AND lower(:name) LIKE ?', lower(#{sanitize("%#{query2.first}%")}) , lower(#{sanitize("%#{query2.last}%")})"
conditions << " lower(:forname) AND lower(:name) LIKE ?', lower(#{sanitize("%#{query2.last}%")}) , lower(#{sanitize("%#{query2.first}%")})"
else
search_columns = [ :forname, :name ]
query2.each do |word|
search_columns.each do |column|
conditions << " lower(#{column}) LIKE lower(#{sanitize("%#{word}%")}) "
end
end
end
conditions = conditions.join('OR')
self.where(conditions)
end
But now i get this error:
SQLite3::SQLException: near "', lower('": syntax error: SELECT "patients".* FROM "patients" WHERE ( lower(:forname) AND lower(:name) LIKE ?', lower('%John%') , lower('%Smith%')OR lower(:forname) AND lower(:name) LIKE ?', lower('%Smith%') , lower('%John%')) LIMIT 12 OFFSET 0
What did i wrong? Thanks!

Resources