Searchlogic OR condition results in undefined method

ε祈祈猫儿з 提交于 2019-12-11 17:47:18

问题


I'm sure that I'm overlooking something since this is my first time using Searchlogic.

Whenever I use a statement like Listing.city_like_or_state_like(params[:search]) in my controller, Rails returns an "Undefined Method" error. I'm trying to search 2 fields within the same model.

However, if I use Listing.city_like(params[:search]) everything is totally fine.

Am I missing something here? I thought OR conditions could be chained together with Searchlogic. How can I implement an OR statement?


回答1:


Searchlogic only supports one "operator" per call. So what you want to do is

Listing.city_or_state_like(params[:search])



回答2:


I think you can use named_scopes and pass the params straight to SearchLogic

models/listing.rb

class Listing < ActiveRecord::Base
    named_scope :city_or_state_like, lambda{|*args| {
                 :conditions => ["city ILIKE ? OR state ILIKE ?", args[0], args[1] ]
                  }
                }

end

controllers/listing_controller.rb

#params for [:search][:city_or_state_like] = [city_var][state_var]
    Listing.search(params[:search])

I'm up-voting aNoble's answer though :D



来源:https://stackoverflow.com/questions/5877439/searchlogic-or-condition-results-in-undefined-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!