# Acts as taggable on steroids - seach all tags

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-on-steroids-seach-all-tags/111267>\
**Category:** Rails\
**Created:** [August 4, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-on-steroids-seach-all-tags/111267 "2007-08-04T16:57:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 4, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-on-steroids-seach-all-tags/111267/1 "2007-08-04T16:57:21Z")

</div>

Hi,

When using acts as taggable on steroids, you can search a model’s tags  
by doing somthing like  
Post.find\_tagged\_with(‘Funny, Silly’)

But how do you search all tags without using manual sql and not limit it  
just to one model?

thanks

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 4, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-on-steroids-seach-all-tags/111267/2 "2007-08-04T19:49:46Z")

</div>

Hi,

> When using acts as taggable on steroids, you can search a model’s tags  
> But how do you search all tags without using manual sql and not limit it  
> just to one model?

When you install the plugin you have the taggings table and the  
corresponding Tagging model. Tagging is related to the model Tag.

So, you could do something like

Tagging.find(:all,:include=\>:tag,:conditions=\>[‘tags.name in  
(?)’,tags\_to\_search])

That will give you all the taggings, having the model name in the  
“taggable\_type” attribute and the id in the taggable\_id field.

Moreover, Tagging has a polymorphic reference to :taggable, meaning you  
could directly get the related objet directly by invoking .taggable over  
any of the row results. Just be aware you’ll be launching a new query  
for each row if you do so since as far as I know you cannot eager load a  
polymorphic association.

Regards,

javier ramírez

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 5, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-on-steroids-seach-all-tags/111267/3 "2007-08-05T15:20:10Z")

</div>

Thanks for the comments above, this is how I have done it. My concern at  
the minute is the 21 database queries that need to made to get the  
results required.

Has anyone got any comments on how this can be optimized?

Thanks

```
#
# searchers all taggable types
def do_all_search(ps_condition, ps_page)
    initial_results = Tagging.paginate(:all,
                           :include=>[:tag],
                           :conditions=>['tags.name in (?)', 

```

ps\_condition],  
:page =\> ps\_page,  
:per\_page =\> 20)

```
    actual_result = Array.new
    initial_results.each do |tag_result|
        actual_result << 

```

tag\_result.taggable.class.find(tag\_result.taggable.id)  
end  
return actual\_result, initial\_results  
end
