Forum: Ruby on Rails How to use polymorphic in a beautiful way in such a case?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d069b3237bc411726d3a83d9077508d?d=identicon&s=25 Charlie (Guest)
on 2006-04-06 14:30
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
This topic is locked and can not be replied to.