# Problem with has\_many :through, :uniq =\> true with polymorph

**URL:** <https://www.ruby-forum.com/t/problem-with-has-many-through-uniq-true-with-polymorph/178863>\
**Category:** Rails\
**Created:** [November 12, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-uniq-true-with-polymorph/178863 "2009-11-12T13:41:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 12, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-uniq-true-with-polymorph/178863/1 "2009-11-12T13:41:26Z")

</div>

Didn’t have quite enough space to describe it there…basically i’m  
having a problem with the :uniq option in my tags.

I’m using acts\_as\_taggable\_on\_steroid which adds these associations to  
my Resource class:

Resource  
has\_many :taggings, :as =\> :taggable, :dependent =\> :destroy, :include  
=\> :tag  
has\_many :tags, :through =\> :taggings, :uniq =\> true

This uses the join model Tagging:

Tagging  
belongs\_to :tag  
belongs\_to :taggable, :polymorphic =\> true

And of course the Tag model:

Tag  
has\_many :taggings, :dependent =\> :destroy  
has\_many\_polymorphs :taggables,  
:from =\> [:resources, :lessons],  
:through =\> :taggings,  
:dependent =\> :destroy

The uniq option doesn’t seem to stop me from adding the same tag  
multiple times to a resource:

> > resource = Resource.first  
> > =\> #\<TeachingObject id: 59, name: “Clarinet bite point close-up” …\>
> 
> > tag = Tag.first  
> > =\> #\<Tag id: 1, name: “recorder”\>
> 
> > resource.tags  
> > =\> [#\<Tag id: 1, name: “recorder”\>, #\<Tag id: 610, name: “chunky  
> > chicken”\>, #\<Tag id: 1, name: “recorder”\>]
> 
> > resource.tags \<\< tag  
> > =\> [#\<Tag id: 1, name: “recorder”\>, #\<Tag id: 610, name: “chunky  
> > chicken”\>, #\<Tag id: 1, name: “recorder”\>, #\<Tag id: 1, name:  
> > “recorder”\>]
> 
> > resource.tags \<\< tag  
> > =\> [#\<Tag id: 1, name: “recorder”\>, #\<Tag id: 610, name: “chunky  
> > chicken”\>, #\<Tag id: 1, name: “recorder”\>, #\<Tag id: 1, name:  
> > “recorder”\>, #\<Tag id: 1, name: “recorder”\>]

Now, i have other classes which use has\_many :through with :uniq =\> true  
and they seem fine: for example. resources also join to assets, through  
a table called ‘items’:

Resource  
has\_many :items, :dependent =\> :destroy, :order =\> :position  
has\_many :assets, :through =\> :items, :order =\> :position, :uniq =\>  
true

Item  
belongs\_to :asset  
belongs\_to :resource

Asset  
has\_many :items, :dependent =\> :destroy, :order =\> :position  
has\_many :resources, :through =\> :items, :order =\> :position

Like i say, this behaves as i’d expect:

> > resource = Resource.first  
> > =\> #\<TeachingObject id: 59, name: “Clarinet bite point close-up” … \>
> 
> > asset = Asset.first  
> > =\> #\<Asset id: 1, name: “07 Clari Clari (notation, perf AI)” … \>
> 
> > resource.assets  
> > =\> []
> 
> > resource.assets \<\< asset  
> > =\> [#\<Asset id: 1, name: “07 Clari Clari (notation, perf AI)” … \>]
> 
> > resource.assets \<\< asset  
> > =\> [#\<Asset id: 1, name: “07 Clari Clari (notation, perf AI)” … \>]

See? It doesn’t add the duplicate.

I’m wondering if it’s to do with the polymorphic nature of the tag -  
resource relationship - maybe the polymorphism is stopping it from  
noticing that it’s not uniq? or something?

Grateful for any help or ideas…i can’t even think of a way to work  
around this since the problem is buried inside active record’s \<\<  
method.
