I am building an app where I’d like to use a simple search to search through the object’s title, AND tags (using acts_as_taggable_on) in one search
I am able to construct either, but not both, and am at my wits end trying to figure it out.
To search using tags, I use:
@post = Post.tagged_with(params[:search])
To search the object, I use:
@post = Post.search(params[:search])
And I wrote a method called search in the Post model, as follows:
def self.search(search)
if search
where('question LIKE ?', "%#{search}%")
else
scoped
end
end
Any idea how to combine these two queries? Any attempts so far have been unsuccessful mainly because there isn’t a “tag” column in my Post model, etc.
I figured it out and testing it in my solution, I wrote the joins myself so it isn’t as clean as I would like so if anybody has a better solution, I’d still love to see it. Here’s what I came up with:
This is working as expected in my tests. I am testing with three records. One has the search term in the title, another has just the tag and the third has both the title and the tag, so it shouldn’t return duplicates.
Hope this helps.