# Polymorphism question

**URL:** <https://www.ruby-forum.com/t/polymorphism-question/169420>\
**Category:** Rails\
**Created:** [June 3, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/polymorphism-question/169420 "2009-06-03T15:50:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 3, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/polymorphism-question/169420/1 "2009-06-03T15:50:52Z")

</div>

I am setting up a tagging system using polymorphism and it works about  
half the way I would expect it to. One thing in my app that can be  
tagged is blogs. I am able to tag them and it works as I expect when I  
save the blog and when I call blog.tags it returns the list of tags  
associated with that blog. The thing that does not work is when I am  
trying to display a list of all the blogs for a certain tag. For  
example:

tag = Tag.find(1)  
tag.blogs ## =\> returns nil

Here is my setup:

class Tag \< ActiveRecord::Base  
end

class Tagging \< ActiveRecord::Base  
belongs\_to :tag  
belongs\_to :taggable, :polymorphic =\> true  
end

class Blog \< ActiveRecord::Base  
has\_many :taggings, :as =\> :taggable  
has\_many :tags, :through =\> :taggings  
end

##when I save a blog in my controller, I do this to assign a tag to it  
blog = Blog.find(1)  
tagging = Tagging.new(:tag\_id=\>tag\_id)  
tagging.taggable = blog  
tagging.save

I have tried experimenting with all kinds of things in the Tag model  
(ex: has\_many :blogs, :through =\>:taggings), but nothing seems to work.  
What am I missing? Maybe I am misunderstanding how polymorphism works  
or maybe there is a better way to set this up in general.

Thanks in advance!  
John

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 3, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/polymorphism-question/169420/2 "2009-06-03T16:54:07Z")

</div>

One of the existing plugins such as acts\_as\_taggable\_on\_steroids might  
have this functionality already… I’m not sure though.

Here’s one way to do it:

class Blog \< ActiveRecord::Base  
has\_many :taggings, :as =\> :taggable  
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 get(taggable)  
self.taggings.all(:conditions =\> [“taggable\_type = ?”,  
taggable.to\_s.singularize.camelize]).map(&:taggable)  
end  
end  
require ‘test\_helper’

class TagTest \< ActiveSupport::TestCase

test ‘should get taggable type’ do  
tag = Tag.create(:name =\> ‘blogtag’)  
blog1 = Blog.create(:name =\> ‘blog1’)  
blog1.tags \<\< tag  
blog2 = Blog.create(:name =\> ‘blog2’)  
blog2.tags \<\< tag

```
assert_equal [tag], blog1.tags
assert_equal [blog1, blog2], tag.get(:blogs)

```

end

end

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 3, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/polymorphism-question/169420/3 "2009-06-03T18:22:51Z")

</div>

Hello Yanni mac,

You can use acts-as-taggable-on plugin for same purpose.

Cheers !  
Sandip

On Wed, Jun 3, 2009 at 8:22 PM, jemminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> self.taggings.all(:conditions =\> [“taggable\_type = ?”,  
> blog1.tags \<\< tag

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.
