views:

32

answers:

1

I'm using ruby on rails 2.3.8, and I've installed the acts_as_taggable_on plugin for tagging announcements and everything was fine until I discovered I had no easy and neat way of getting all the announcements tagged with a certain id(the only method I found was Tag.tagged_with(tag_name)).

So, I just wanted to relate the Tag model of the plugin, to a Tagging model I created (of ActiveRecord), representing the taggings table that is included within the plugin.

Is it possible to do that?

A: 

It should be possible, if they are both ActiveRecord models.

To define the relationship in the plugin's model you'll have to do some metaprogramming somewhere in a library. You can stick it at the end of your environment.rb file at first to experiment.

ModelName.class_eval do
  has_many :announcements
end

That in particular may or may not work but that's the basic idea.

If the metaprogramming proves to be too complicated, you can always just do (making assumptions about the schema here, I haven't used AAT in a few year):

Taggings.find_by_tag_id_and_user_id(@tag.id, @user.id, :include => :user ).map{ |t| t.user) }

John
I've got a model called Announcement, and I'd like to get all the announcements tagged with a certain tag_id. The only method I found to accomplish that is "tagged_with(tag_name)", and not with "tag_id" as an argument. I can't depend on having every tag with different names, I want to find them by id.
Brian Roisentul
k, i embellished my answer, hopefully that is helpful.
John