# Has many, belongs to relationship allowing duplicates

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775>\
**Category:** Rails\
**Created:** [February 16, 2007, 7:08am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775 "2007-02-16T07:08:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 16, 2007, 7:08am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775/1 "2007-02-16T07:08:53Z")

</div>

Hi all,

I have a couple of tables setup as follows:

(1) User table (general user info)

(2) Subsriptions table  
id  
user\_id \<- links to User.id  
subscriber\_id \<- links to User.id

Basically a user can have one or more subscribers. Subscribers being  
other users.

User has the following:

has\_many :subscribers,  
:class\_name =\> ‘Subscription’

Subscription model has:

belongs\_to :user,  
:class\_name =\> ‘User’

Is there anyway that I can setup a relationship like this, with these  
two tables, having a user that has subscribers, but disallow  
duplicates in the subscriptions table?

Because when I do something line the following:

u = User.find(1)  
s = Subscription.new  
s.user\_id = 1  
s.subscriber\_id = 3  
u.subscribers \<\< 2

I end up with the following records in the subscriptions table:

id user\_id subscriber\_id  
1 1 3  
2 1 3

Record with id = 1 was there before the above code was run. How can I  
set thiings up so that if I add a subscriber that already exists for a  
particular user, it doesn’t add a duplicate record. Would be nice if  
it doesn’t complain about trying to add one either and just does  
nothing if it’s already there. Maybe that’s asking too much?

Any help would be greatly appreciated.

Thanks in advance.

Cheers,  
Diego

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 16, 2007, 7:37am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775/2 "2007-02-16T07:37:55Z")

</div>

Diego,

Take a look at validates\_uniqueness\_of. Something like this should  
get you started:

class Subscription \< ActiveRecord::Base  
validates\_uniqueness\_of :user\_id, :scope =\> :subscriber\_id  
end

Validations will prevent a record from being saved and attach error  
information to the object. What you do with that information is your  
decision.

Aaron

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 16, 2007, 7:40am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775/3 "2007-02-16T07:40:55Z")

</div>

Hi Arron,

Thanks for the pointers. I’ll follow along and try these out.  
Appreciate it!

Cheers,  
Diego

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 16, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-relationship-allowing-duplicates/85775/4 "2007-02-16T17:36:32Z")

</div>

> other users.
> 
> Is there anyway that I can setup a relationship like this, with these  
> two tables, having a user that has subscribers, but disallow  
> duplicates in the subscriptions table?

has\_many has a :uniq option that might help you out…

:uniq - if set to true, duplicates will be omitted from the collection.  
Useful in conjunction with :through.
