So my Project model currently contains a column named tags which are in the format of a,b,c,d,e,f,etc... In the view, I have a form which collects checkboxes's into filters[] which I can use in the controller with params[:filters]. My question is how do I filter the projects that contains at least one of those tags?
def index
@search = Project.search(params[:search])
@projects = @search.???
end
I thought something like
@projects = Project.search(:tags_contains => params[:filters])
or
params[:filters].each do |p|
@projects ||= @search.where(["projects.tags like ?", p])
end
would work, but it doesn't seem to return anything. Any advice?
Example: Let's say checkboxes a and c were checked in the form which would be contained inside the params[:filters]. I have three projects:
Project | Tags
------------+------
Project 1 | a,b
Project 2 | c
Project 3 | a,c
Project 4 | d
In this example I would want to return Project 1,2,3 since they contain at least a or c
Figured it out using the squeel gem...
params[:filters].each do |p|
@projects ||= @search.where(["projects.tags like ?", "%#{p}%"])
end
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.