Searchlogic OR condition results in undefined meth

2019-08-22 11:38发布

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?

2条回答
来,给爷笑一个
2楼-- · 2019-08-22 11:51

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

查看更多
叼着烟拽天下
3楼-- · 2019-08-22 12:05

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

Listing.city_or_state_like(params[:search])
查看更多
登录 后发表回答