Dynamic "OR" conditions in Rails 3
Posted
by
Ryan Foster
on Stack Overflow
See other posts from Stack Overflow
or by Ryan Foster
Published on 2011-01-06T13:51:25Z
Indexed on
2011/01/06
13:53 UTC
Read the original article
Hit count: 305
I am working on a carpool application where people can search for lifts. They should be able to select the city from which they would liked to be picked up and choose a radius which will then add the cities in range to the query. However the way it is so far is that i can only chain a bunch of "AND"
conditions together where it would be right to say "WHERE start_city = city_from OR start_city = a_city_in_range OR start_city = another_city_in_range"
Does anyone know how to achive this? Thanks very much in advance.
class Search < ActiveRecord::Base
def find_lifts
scope = Lift.where('city_from_id = ?', self.city_from)
#returns id of cities which are in range of given radius
@cities_in_range_from = City.location_ids_in_range(self.city_from, self.radius_from)
#adds where condition based on cities in range
for city in @cities_in_range_from
scope = scope.where('city_from_id = ?', city)
#something like scope.or('city_from_id = ?', city) would be nice..
end
end
© Stack Overflow or respective owner