How to use polymorphic in a beautiful way in such a case?


#1

class User<ActiveRecord::Base
has_many :taggings, :as=>:taggable,:dependent=>true
has_many :tags,:through=>:taggings
end
class Article<ActiveRecord::Base
has_many :taggings, :as=>:taggable,:dependent=>true
has_many :tags,:through=>:taggings
end
class Tagging<ActiveRecord::Base
belongs_to :tag
belongs_to :taggable,:polymorphic=>true
end
class Tag<ActiveRecord::Base
has_many :taggings
def tagged
taggings.collect{|tagging| tagging.taggable}
end

end

The above code is something like acts_as_taggable plugin.
Now I want this function:Given a tag,then list all the articles that has
such a tag.Traditionally,the plugin just provides attaching tags to
users and articles,but this function need the reverse relation.
The provided “tagged” function:
david=User.find(…)
john=User.find(…)
welcome=Article.find(1)

then “david” has tag “summer” and “john” ,“welcome” also have the tag
“summer”

summer.tagged #=>[david,john,welcome]

I only want to get david,john while not welcome

now I have to do that using find_by_sql,very ugly,is there a good way?

Thanks
Charlie