I want to do search with multiple string like below using ransack
SELECT "users".* FROM "users" WHERE email LIKE 'kin%' OR name LIKE 'jeno%' ORDER BY 1;
so i have tried with 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
But i failed to get the result,so please giude me
Currently am using like
User.where("email like ? or email like ?", "%abc", "%ddd")
How to do with ransack
?
I think ransack
can't solve this.
# 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
argment is like { :email_cont => ["kingston@jenorish.com","admin@jenorish.com"] }
In build
method,just call send("email_cont=", ["kingston@jenorish.com","admin@jenorish.com"])
So you just do as below:
User.where("email like ? or email like ?", "%abc", "%ddd")
# SQL is ...
# => SELECT "users".* FROM "users" WHERE (email like '%abc' or email like '%ddd')
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.