Newbie: tags (or habtm) howto?

I there some place that has a good walk through on implementing
tagging?

Basically, I have a table of things a table of tags. the things have a
many-to-many relationship to tags. I can get tags for a thing by
thing.tags but i’m at a loss as to how to get things back that are
tagged with multiple tags –

ie. if i were writing sql:

SELECT thing_id
FROM thing
JOIN thing_tag AS thing_tag_1 ON (thing.thing_id =
thing_tag_1.thing_id)
JOIN tag AS tag_1 ON (thing_tag_1.tag_id = tag_1.tag_id)
JOIN thing_tag AS thing_tag_2 ON (thing.thing_id =
thing_tag_2.thing_id)
JOIN tag AS tag_2 ON (thing_tag_2.tag_id = tag_2.tag_id)
WHERE tag_1.tag = ‘foo’
AND tag_2.tag = ‘bar’

help!! thanks!

Hi,

Just use acts_as_taggable, it’s easy and works great.

http://wiki.rubyonrails.com/rails/pages/Acts+As+Taggable+Plugin

enjoy.

Thanks, Elad. It seems like there’s some question as to whether to use
the gem or the plug-in. Any thoughts?

FYI, for anyone else dickering around with this sort of thing, I found
this helpful:
http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html

Intersection (AND)
Query for “bookmark+webservice+semweb”:
SELECT b.*
FROM scBookmarks b, scCategories c
WHERE c.bId = b.bId
AND (c.category IN (‘bookmark’, ‘webservice’, ‘semweb’))
GROUP BY b.bId
HAVING COUNT( b.bId )=3

the having count() thing is cute.

I currently use the acts_as_taggable plugin because I need to tag
stuff on multiple tables and if you use the gem version you need a
taggings tables for each one of those tables. So if you need to tag a
lot of different I’d go with the plugin, or else the gem which has
some goodies built-in like tags cloud.

Unfortunately, both seem to be have no more development at all :frowning:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs