简体   繁体   中英

Searchlogic OR condition results in undefined method

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?

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

Listing.city_or_state_like(params[:search])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM