# Has\_and\_belongs\_to\_many tags

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-tags/54510>\
**Category:** Rails\
**Created:** [March 7, 2006, 11:59am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-tags/54510 "2006-03-07T11:59:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_MacCaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Alexander\_MacCaw](https://www.ruby-forum.com/u/Alexander_MacCaw)\
**Post date:** [March 7, 2006, 11:59am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-tags/54510/1 "2006-03-07T11:59:17Z")

</div>

I’m trying to create a search where I can find pages by their tag name.  
Here’s a sample of my code (not working).

@tags = Page.find(:all, :conditions =\> [“#{:tag.name} LIKE ?”,‘%’ +  
@params[‘searchtags’] + ‘%’ ])

My enviroment:  
I’ve got a table called tags, and a table called pages, both with a  
relationship of has\_and\_belongs\_to\_many. I also have a table called  
pages\_tags. Basically my tables are set up like here:  
[http://www.railsdiary.com/diary/tagging\_has\_and\_belongs\_to\_many](http://www.railsdiary.com/diary/tagging_has_and_belongs_to_many)

Would appreciate some help on how I can reference a tag through a Page.

---

<div class="post-metadata">

**Author:** ![Alexander\_MacCaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Alexander\_MacCaw](https://www.ruby-forum.com/u/Alexander_MacCaw)\
**Post date:** [March 7, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-tags/54510/2 "2006-03-07T18:01:47Z")

</div>

Am Dienstag, den 07.03.2006, 11:59 +0100 schrieb Alexander MacCaw:

> I’m trying to create a search where I can find pages by their tag name.  
> Here’s a sample of my code (not working).
> 
> @tags = Page.find(:all, :conditions =\> [“#{:tag.name} LIKE ?”,‘%’ +  
> @params[‘searchtags’] + ‘%’ ])

Why is your instance variable call @tags, if you are looking for pages?

> My enviroment:  
> I’ve got a table called tags, and a table called pages, both with a  
> relationship of has\_and\_belongs\_to\_many. I also have a table called  
> pages\_tags. Basically my tables are set up like here:  
> [http://www.railsdiary.com/diary/tagging\_has\_and\_belongs\_to\_many](http://www.railsdiary.com/diary/tagging_has_and_belongs_to_many)
> 
> Would appreciate some help on how I can reference a tag through a Page.

You post is a little confusing, but i guess you want to do the  
following:

@pages = Page.find(  
:all,  
:include =\> :tags,  
:conditions =\> [‘tags.name LIKE ?’, “%#{params[:searchtags]}%”]  
)

This will work, if you have a search string like ‘boat’ or ‘strawber’,  
just a single tag, partial or complete.

If you have multiple tags in your search string, you should separate  
them before querying:

# will split on whitespaces

tags = params[:searchtags].split(/\s/)  
@pages = Page.find(  
:all,  
:include =\> :tags,  
:conditions =\> [‘tags.name IN (?)’, tags.join(‘,’)]  
)

## This will only work for complete tags.

Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)
