# Has\_many\_polymorphs for user owned tags

**URL:** <https://www.ruby-forum.com/t/has-many-polymorphs-for-user-owned-tags/121612>\
**Category:** Rails\
**Created:** [November 29, 2007, 10:54am UTC](https://www.ruby-forum.com/t/has-many-polymorphs-for-user-owned-tags/121612 "2007-11-29T10:54:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [November 29, 2007, 10:54am UTC](https://www.ruby-forum.com/t/has-many-polymorphs-for-user-owned-tags/121612/1 "2007-11-29T10:54:33Z")

</div>

Hi all,

I’m trying to extend the tagging functionality of has\_many\_polymorphs to  
work with user owned tags - ie, putting belongs\_to :user in the Tagging  
model.

So I changed the index in the generated migration, and added user\_id to  
the taggings table.

Then I added belongs\_to :user in the Tagging model.

In the lib/tagging\_extensions.rb file I added a new function:

```
def _add_tags_from_user incoming, user
  taggable?(true)
  tag_cast_to_string(incoming).each do |tag_name|
    begin
      tag = Tag.find_or_create_by_name(tag_name)
      raise Tag::Error, "tag could not be saved: #{tag_name}" if

```

tag.new\_record?  
Tagging.create(:tag =\> tag, :taggable =\> self, :user\_id =\>  
user.id)  
rescue ActiveRecord::StatementInvalid =\> e  
raise unless e.to\_s =~ /duplicate/i  
end  
end  
end

My first problem is that if a two users tag an object with the same tag,  
then I get that tag returned twice - even when I’ve added :uniq =\> true  
to the has\_many\_polymorphs directive in the Tag model.

My second is that I’m unsure about how to modify the  
tagging\_extensions.rb to remove user owned tags. Has anyone done this  
before?

Any suggestions, greatly appreciated!

Cheers,

Tom
