简体   繁体   English

Searchlogic OR 条件导致未定义的方法

[英]Searchlogic OR condition results in undefined method

I'm sure that I'm overlooking something since this is my first time using Searchlogic.我确信我忽略了一些东西,因为这是我第一次使用 Searchlogic。

Whenever I use a statement like Listing.city_like_or_state_like(params[:search]) in my controller, Rails returns an "Undefined Method" error.每当我在 controller 中使用类似Listing.city_like_or_state_like(params[:search])的语句时,Rails 都会返回“未定义方法”错误。 I'm trying to search 2 fields within the same model.我正在尝试在同一个 model 中搜索 2 个字段。

However, if I use Listing.city_like(params[:search]) everything is totally fine.但是,如果我使用Listing.city_like(params[:search])一切都很好。

Am I missing something here?我在这里错过了什么吗? I thought OR conditions could be chained together with Searchlogic.我认为 OR 条件可以与 Searchlogic 链接在一起。 How can I implement an OR statement?如何实现 OR 语句?

Searchlogic only supports one "operator" per call. Searchlogic 每次调用仅支持一个“操作员”。 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我认为您可以使用 named_scopes 并将参数直接传递给 SearchLogic

models/listing.rb模型/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控制器/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不过,我赞成 aNoble 的回答:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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