Brian Roisentul
Brian Roisentul

Reputation: 4750

Find tag by id using acts_as_taggable_on

I'm using Ruby on rails 2.3.8 and acts_as_taggable_on plugin. This plugin generates three tables: tags(which has the id and name of each tag) and taggings(relates tag_ids which the items that are tagged).

Now, I'd like to get all the items tagged with a certain tag_id. So, I go to my Announcement model(the announcements are the tagged items) and I'd like to write something like this:

def find_by_category(tag_id)
  Announcement.tagged_with(tag_id) #this doesn't exist.
end

But the only method I found actually was this:

Announcement.tagged_with(tag_NAME) #this works, but I don't want to find by a name.

Does anybody know a method to accomplish this, or at least, how to modify the plugin's code to do this?

Upvotes: 1

Views: 1334

Answers (2)

Aaron Davis
Aaron Davis

Reputation: 91

tag = ActsAsTaggableOn::Tag.find(tag_id)  
Announcement.tagged_with(tag)

A year later, I hope this is helpful for someone!

Upvotes: 8

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

acts_as_taggable has a method

Model.find_tagged_with @tag_name

That works for me in a couple of projects.

Method docs

Upvotes: 0

Related Questions