I trying to create a select using Arel but I’m passing three parameters,
I
want to include the parameters only if they are different to nil or
blank.
Here is my method:
def self.advsearch(summary_description, specialties, place)
User.joins(:experience,:summary,:information)
.where(Information.arel_table[:business].eq(false))
.where(Experience.arel_table[:description].matches("%#{summary_description}%")
.or(Experience.arel_table[:description].matches("%#{summary_description.capitalize}%"))
.or(Experience.arel_table[:job_title].matches("%#{summary_description}%"))
.or(Experience.arel_table[:job_title].matches("%#{summary_description.capitalize}%"))
.or(Summary.arel_table[:summary_description].matches("%#{summary_description}%"))
.or(Summary.arel_table[:specialties].matches("%#{specialties}%"))
.or(Information.arel_table[:address].matches("%#{place}%")))
.order
.uniq
end
I would like to do something like this:
def self.advsearch(summary_description, specialties, place)
User.joins(:experience,:summary,:information)
.where(Information.arel_table[:business].eq(false))
.where(Experience.arel_table[:description].matches("%#{summary_description}%")
unless !summary_description.nil?
.or(Experience.arel_table[:description].matches("%#{summary_description.capitalize}%"))
unless !summary_description.nil?
.or(Experience.arel_table[:job_title].matches("%#{summary_description}%"))
unless !summary_description.nil?
.or(Experience.arel_table[:job_title].matches("%#{summary_description.capitalize}%"))
unless !summary_description.nil?
.or(Summary.arel_table[:summary_description].matches("%#{summary_description}%"))
unless !summary_description.nil?
.or(Summary.arel_table[:specialties].matches("%#{specialties}%"))
unless !summary.nil?
.or(Information.arel_table[:address].matches("%#{place}%")))
unless !place.nil?
.order
.uniq
end
I trying to google something similar, but I didn’t find anything like
that.
Thanks in advance for your help.