2011-12-17 5 views
0

안녕하세요 간단한 검색 엔진을 쓰려고합니다. 내가 좋아하는 URL을 얻을 :필터가있는 레스토랑 검색

.../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 

을하지만 난 정말 그것이 작동되도록 방법을 모르겠어요. 이와

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 

: 나는 결과가있을 때

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 

내가하여 필터링 싶습니다

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

레스토랑, 도시 및 지역 모델

하지만 작동하지 않습니다. 내가 어떻게이 일을 할 수 있는지 조언 해 줄 수 있니?

+0

내 심령 팬츠를 입히게하고 왜 그렇게 잘 작동하지 않는지 알게 될 것입니다. – Trip

답변

0

처음부터 자신의 코드를 롤링하는 대신 시작 지점으로 Searchlogic을 사용하는 것이 좋습니다.

+0

고마워, 레일즈 3.1의 Searchlogic에는 문제가 있지만 Squeel을 발견하고 나에게 도움이된다. –

+0

오, 안돼. Searchlogic을 좋아하지만 아직 Rails 3.1을 사용하지 않았습니다. Squeel에 대한 팁 주셔서 감사합니다. – jdl

관련 문제