带有搜索结果的多字符串搜索

时间:2015-02-05 13:36:54

标签: ruby-on-rails activerecord ransack

我想使用ransack

使用下面的多个字符串进行搜索

SELECT "users".* FROM "users" WHERE email LIKE 'kin%' OR name LIKE 'jeno%' ORDER BY 1;

所以我尝试了ransack

1)User.search(:email_cont => ["kingston@jenorish.com","admin@jenorish.com"]).result

2)User.search(:email_cont => "kingston@jenorish.com","admin@jenorish.com").result

但是我没有得到结果,所以请给我打电话。

目前正在使用

User.where("email like ? or email like ?", "%abc", "%ddd")

如何处理ransack

1 个答案:

答案 0 :(得分:0)

我认为ransack无法解决这个问题。

# search.rb on ransack gem
def build(params)
  collapse_multiparameter_attributes!(params).each do |key, value|
    if Constants::S_SORTS.include?(key)
      send("#{key}=", value)
    elsif base.attribute_method?(key)
      base.send("#{key}=", value)
    elsif @context.ransackable_scope?(key, @context.object)
      add_scope(key, value)
    elsif !Ransack.options[:ignore_unknown_conditions]
      raise ArgumentError, "Invalid search term #{key}"
    end
  end
  self
end

params参数类似于{ :email_cont => ["kingston@jenorish.com","admin@jenorish.com"] }

build方法中,只需致电send("email_cont=", ["kingston@jenorish.com","admin@jenorish.com"])

所以你只需按以下步骤操作:

User.where("email like ? or email like ?", "%abc", "%ddd")
# SQL is ...
# =>  SELECT "users".* FROM "users" WHERE (email like '%abc' or email like '%ddd')