2011-01-06 83 views
0

我正在拼车应用程序中,人们可以搜索电梯。他们应该能够选择他们希望从中选择的城市,并选择一个半径,然后将范围内的城市添加到查询中。然而,目前为止,我只能将一堆"AND"条件链接在一起,在那里它可以说"WHERE start_city = city_from OR start_city = a_city_in_range OR start_city = another_city_in_range"Rails 3中的动态“OR”条件

有没有人知道如何实现这个?首先十分感谢。

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 
+0

惯于这种搜索管芯的多次并行请求时,你去住吗?考虑到你在每一行有10-12个参数的线性搜索?管理复杂的数据库暨Web服务器拓扑将成为系统管理员的噩梦。 – Siddharth 2012-07-02 18:52:17

回答

2

你可以用“IN”操盘手“=”,而无需使用SQL语法 随着阿雷尔(由Rails的3.0中使用),你可以做这样

class Search < ActiveRecord::Base 

    def find_lifts  

    #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) 
    scope = Lift.where{ :city_from_id => [self.city_from] + @cities_in_range_from }) 

    # ... and so on 
    end 
end 
+0

像一个迷人的作品。谢谢! – 2011-01-06 15:22:41