I have a where clause being called based off of a json object from a form. The object has a series of boolean values for locales which will be used to find Venues within the given locale.
Is there a better way to write a series of queries than having a long string built off of boolean values? Any help in the right direction would be greatly appreciated. Thanks!
hard_worker.rb
def build_locales
filters = []
filters.push 'locale_north' if @lead.locale_north
filters.push 'locale_south' if @lead.locale_south
filters.push 'locale_east' if @lead.locale_east
filters.push 'locale_west' if @lead.locale_west
return filters
end
def build_string
filters = build_locales
s = ''
filters.each_with_index do |f, i|
s+= "#{f} = true"
s+= " OR " if i < filters.size - 1
end
end
def perform(lead_id)
@venues = Venue.where(receive_all: true).or(Venue.where(build_string))
// ... more code ...
end
where
clauses are chainable so you can easily do a lot of querying if you need to with an approach like this.
@venues = Venue.all
@venues = @venues.where(receive_all: true)
filters.each do |filter|
@venues = @venues.or(Venue.where(filter.to_sym: true))
end
# after all of this you can just return @venues
@venues