搜索带过滤器的餐馆

时间:2011-12-17 17:12:57

标签: ruby-on-rails ruby

您好我正在尝试编写简单的搜索引擎。我得到网址:

.../fragments/get_restaurants?city=London&service=1,2

我希望像这样搜索:

def get_restaurants 
    regions_find = Region.find(:all, :select => "id", :conditions => ["name LIKE ?", "#{params[:city]}"]).restaurants
    cities_find = City.find(:all, :select => "id", :conditions  => ["name LIKE ?", "%#{params[:city]}%"]).restaurants
    regions_and_cities = reg.merge!(cit).uniq
    restaurans_all = regions_and_cities.find(:all, :conditions => ["name LIKE ?", "%#{params[:name]}%" OR TYPE OR KIND])

    filtered = filter(restaurans_all)

    render :partial => "fragments/restaurant", :collection => res
end

但我真的不知道如何使它发挥作用。

这是餐馆,城市和地区的模型:

class Region < ActiveRecord::Base  
  has_many :cities, :dependent => :destroy # dependent only with has_
  has_many :restaurants
end

class Restaurant < ActiveRecord::Base
  belongs_to :region
  belongs_to :city
  has_many :restaurants_types
  has_many :types, :through => :restaurants_types
end

class City < ActiveRecord::Base
  belongs_to :region
  has_many :restaurants
end

当我得到结果时我想过滤它:

def filter(table)
    res = table
    res &= table.filter('types', params[:type].split(','))          unless params[:type].nil?
    res &= table.filter('cuisines', params[:cuisine].split(','))    unless params[:cuisine].nil? 
    res &= table.filter('facilities', params[:facility].split(',')) unless params[:facility].nil? 
    res &= table.filter('prices', params[:price].split(','))        unless params[:price].nil?
    return res
  end

用这个:

scope :filter, lambda{|type_name, type_id| includes(type_name.to_sym).where(["#{type_name}.id in (?)", type_id]) }

但它不起作用。你能告诉我一些如何做到这一点的建议吗?

1 个答案:

答案 0 :(得分:0)

我建议使用Searchlogic作为起点,而不是从头开始编写自己的代码。