我想使用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
?
答案 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')