views:

38

answers:

2

So I have two separate queries:

tagged_items = Item.tagged_with(params[:s], :on => :tags)
searched_items = Item.find(:all, :conditions => ["MATCH(title) AGAINST (? IN BOOLEAN MODE)", "*#{params[:s]}*"])

The first tagged_items is using the acts_as_taggable_on plugin to find all the items tagged with XYZ.

The second, searched_items, is used to search the items table for the search term.

So, how could I combine (and avoid duplicates) the results of these two?

A: 
items = (tagged_items + searched_items).unique

But it would be much better if you could fetch them with single query.

klew
+2  A: 

Check out named_scope. The second query can be converted to named_scope easily, I'm not sure about the first one, but if you can rewrite it using find, you're home.

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

psyho