Combining meta_search with acts_as_taggable_on
Asked Answered
U

2

5

I've run into a small problem with some search-functionality for a Rails 3 website I'm developing. I have a simple Post model that looks like this:

class Post < ActiveRecord::Base
  acts_as_taggable
end

I'm using acts_as_taggable_on to make adding tags to my posts a bit easier. When I have a post tagged 'rails' and I do the following, all works well:

@posts = Post.tagged_with("rails")

Thing is, I also want to search for the title of the post. When I have a post titled 'Hello world' and tagged 'rails', I want to be able to find this post by searching for 'hello' or 'rails'. So I want a LIKE statement for the title column in combination with the tagged_with method acts_as_taggable_on provides. The where scope doesn't work, because it uses AND instead of OR.

I hoped meta_search would fix the problem, but this isn't working for me. I tried several things. Here are two examples of what I tried:

@search = Post.search(:tagged_with_or_title_like => params[:search])
@search = Post.search(:title_like => params[:search], :tagged_with => params[:search])

It just doesn't recognize 'tagged_with'. It's my first time using meta_search, so I might be doing something wrong here. ;) I've read somewhere that searchlogic worked in combination with acts_as_taggable_on, but since it doesn't support Rails 3, I can't use that.

I hoped somebody here could help me with this problem. Anyone know how to combine acts_as_taggable_on with meta_search? Or maybe know a solution without meta_search? Also, if there's a better option for acts_as_taggable_on that works with meta_search I'd love to hear that too. :)

EDIT: I got it working without using tagged_with or meta_search. It looked like this:

Post.select("DISTINCT posts.*").joins(:base_tags).where("posts.title LIKE ? OR tags.name = ?", "%"+params[:search]+"%", params[:search])

The query created was ridiculous, so I tried a different route: without acts_as_taggable_on and meta_search. I created a tags table myself and connected it to the posts with has_and_belongs_to_many. I won't have other tables that need to be connected to the tags, so this will do the trick for me. I created a scope for searching both the tags and title:

scope :search, lambda { |search| select("DISTINCT posts.*").joins(:tags).where("posts.title LIKE ? OR tags.name = ?", "%"+search+"%", search) }

Now I can do the following:

Post.search(params[:search])

It works great and the query is also quite nice. Still, if anyone knows a better way: please tell me. It could also be helpful for the people coming here via Google.

Undergraduate answered 23/4, 2011 at 19:36 Comment(1)
I updated the question with how I solved it. I eventually just dropped acts_as_taggable and meta_search. Maybe you could try Serge's answer. If it works I'll mark it as answered. :)Undergraduate
T
7
Post.metasearch({:title_or_tag_taggings_tag_name_contains => params[:search]})

enjoy

Tana answered 8/7, 2011 at 17:28 Comment(1)
I just tested if this works inside my old project and it works! So I'm marking this as answered. Thank you. :)Undergraduate
S
1

I think the solution you're looking into is using database VIEW as new model.

You can create view containing join tables Post and Tags.

Then you can search it, using meta_search without problems.

Sherasherar answered 24/4, 2011 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.