2011-12-17 59 views
0

嗨,我想寫簡單的搜索引擎。我得到的網址,如:搜索帶過濾器的餐廳

.../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]) } 

但它不工作。你能給我一些提示我怎麼能做到這一點?

+0

讓我穿上我的通靈褲,然後我會知道爲什麼它不太好。 – Trip 2011-12-17 22:48:51

回答

0

而不是從頭開始編寫你自己的代碼,我建議使用Searchlogic作爲起點。

+0

謝謝,但在rails 3.1中有一個與Searchlogic有關的問題,但是我發現了Squeel,它對我很好用 – 2011-12-17 21:05:40

+0

哦,不,我喜歡Searchlogic,但還沒有使用Rails 3.1。感謝Squeel的提示。 – jdl 2011-12-17 21:48:51