我有一个查询,搜索同一个表中的两个单独的字段…寻找最有可能是特定城市的地点,但也可能是一个国家,即需要两个字段.
表格如下所示:
Country City Germany Aachen USA Amarillo USA Austin
结果:
Keyword Sideinfo Aachen Germany USA Country Austin USA Germany Country
基本上我想知道是否有一个更简洁的方式来做到这一点,因为我不得不使用两个单独的查询,然后将它们添加在一起,排序等(这是正常的):
def self.ajax(search) countries = Location.find(:all,:select=> 'country AS keyword,"Country" AS sideinfo',:joins => :hotels,:conditions => [ 'hotels.email IS NOT NULL AND country LIKE ?',"#{search}%" ],:group => :country ) cities = Location.find(:all,:select=> 'city AS keyword,country AS sideinfo',:conditions => [ 'hotels.email IS NOT NULL AND city LIKE ?',:group => :city ) out = cities + countries out = out.sort { |a,b| a.keyword <=> b.keyword } out.first(8) end
我找不到任何关于如何使用ActiveRecord工会的信息…