# Problem with uniq option in has\_many\_polymorphs

**URL:** <https://www.ruby-forum.com/t/problem-with-uniq-option-in-has-many-polymorphs/190951>\
**Category:** Rails\
**Created:** [July 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/problem-with-uniq-option-in-has-many-polymorphs/190951 "2010-07-01T14:10:20Z")\
**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:** [July 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/problem-with-uniq-option-in-has-many-polymorphs/190951/1 "2010-07-01T14:10:20Z")

</div>

hi all

I have a has\_many\_polymorphs relationship that involves the following  
classes:

class DigilearningModule \< ActiveRecord::Base  
has\_many\_polymorphs :members,  
:from =\> [:digilearning\_titles, :lesson\_categories,  
:lesson\_category\_groups],  
:through =\> :digilearning\_module\_memberships,  
:dependent =\> :destroy,  
:uniq =\> true  
end

class DigilearningModuleMembership \< ActiveRecord::Base  
belongs\_to :digilearning\_module  
belongs\_to :member, :polymorphic =\> true  
acts\_as\_list :scope =\> :digilearning\_module  
end

The classes listed in the from option have no further associations with  
DigilearningModule or DigilearningModuleMembership, besides the ones  
dynamically added by has\_many\_polymorphs.

As you can see, :uniq is set. This uniqueness is enforced on the  
has\_many relationship created from DigilearningModule to (eg)  
DigilearningTitle, ie if i add a digilearning\_title to a  
digilearning\_module multiple times, then the digilearning\_module still  
only has one digilearning\_title. This is fine.

However, the uniqueness isn’t enforced in the opposite direction. I can  
add a digilearning\_module to the same digilearning\_title multiple times.

Before i go and start trying to monkey patch the has\_many\_polymorphs  
code to add this uniqueness to the dynamically created has\_many  
associations it adds to its target classes, can anyone tell me  
a) if this is normal  
b) is there an easy fix?

thanks - max
