# Validations and has\_many :through (Join Models)

**URL:** <https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547>\
**Category:** Rails\
**Created:** [April 11, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547 "2006-04-11T22:14:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 11, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547/1 "2006-04-11T22:14:11Z")

</div>

Hello again:

I’m hoping that somebody, anybody, can help with this question.

How do I make sure that records in a join model are unique?

Using the scenario outlined in Chad F.'s “Rails Recipies”, number  
16, “Many to Many Relationships Where the Relationship Iteself Has  
Data”.

Here are the tables:

create\_table :magazines do |t|  
t.column :title, :string  
end

create\_table :readers do |t|  
t.column :name, :string  
end

create\_table :subscriptions do |t|  
t.column :reader\_id, :integer  
t.column :magazine\_id, :integer  
t.column :last\_renewal\_on, :date  
t.column :length\_in\_issues, :integer  
end

And here are the models:

class Subscription \< ActiveRecord::Base  
belongs\_to :reader  
belongs\_to :magazine  
end

class Reader \< ActiveRecord::Base  
has\_many :subscriptions  
has\_many :magazines, :through =\> :subscriptions  
end

class Magazine \< ActiveRecord::Base  
has\_many :subscriptions  
has\_many :readers, :through =\> :subscriptions  
end

Here’s the code I’m using to test:

magazine = Magazine.create(:title =\> “Ruby Illustrated”)  
reader = Reader.create(:name =\> “Anthony Braxton”)  
subscription = Subscription.create(:last\_renewal\_on =\> Date.today,  
:length\_in\_issues =\> 6)  
reader.subscriptions \<\< subscription  
magazine.subscriptions \<\< subscription  
subscription.save  
subscription2 = Subscription.create(:last\_renewal\_on =\> (Date.today+1),  
:length\_in\_issues =\> 12)  
reader.subscriptions \<\< subscription2  
magazine.subscriptions \<\< subscription2  
subscription2.save

After all of that, there now exists 2 records in the subscriptions  
table, both of which have the same reader\_id and magazine\_id. How do  
you perform the validation to make sure that each record in the  
subscriptions table has a unique reader\_id, magazine\_id pair?

Thanks.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 11, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547/2 "2006-04-11T22:57:08Z")

</div>

Hello David,

[…]

> After all of that, there now exists 2 records in the subscriptions  
> table, both of which have the same reader\_id and magazine\_id.  
> How do you perform the validation to make sure that each record  
> in the subscriptions table has a unique reader\_id, magazine\_id  
> pair?

Try this :

class Subscription \< ActiveRecord::Base  
belongs\_to :reader  
belongs\_to :magazine

validates\_uniqueness\_of :reader\_id, :scope =\> :magazine\_id  
end

Good night,

```
-- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 11, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547/3 "2006-04-11T23:27:49Z")

</div>

Jean-FranÃ§ois wrote:

> Hello David,
> 
> […]
> 
> > After all of that, there now exists 2 records in the subscriptions  
> > table, both of which have the same reader\_id and magazine\_id.  
> > How do you perform the validation to make sure that each record  
> > in the subscriptions table has a unique reader\_id, magazine\_id  
> > pair?
> 
> Try this :
> 
> class Subscription \< ActiveRecord::Base  
> belongs\_to :reader  
> belongs\_to :magazine
> 
> validates\_uniqueness\_of :reader\_id, :scope =\> :magazine\_id  
> end
> 
> Good night,
> 
> ```
> -- Jean-FranÃ§ois.
> 
> ```

Thanks Jean-FranÃ§ois!

Originally I tried putting a validates\_uniqueness\_of in the Subscription  
model, but I used validates\_uniqueness\_of :reader, :scope =\> :magazine.  
Of course it didn’t work. And I didn’t think of using the id’s  
directly, because I was not thinking and didn’t want to put database  
details in if I didn’t have to. Duh, the model is where those sort of  
things are supposed to go!

As I said, thanks again.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 12, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/validations-and-has-many-through-join-models/57547/4 "2006-04-12T14:45:31Z")

</div>

With ref to this post, will using a naming convention of  
magazines\_subscriptions yield any reduction in effort? Or is the  
through used because the subscription table is named outside of the  
convention?

IM having a very hard time accessing and displaying joined data…i  
thought i might have got my head round it after the first 40  
hours…grrrr…

I will post more on my specific problem if i dont come across an answer  
in the archive…

David W. wrote:

> Jean-FranÃ§ois wrote:
> 
> > Hello David,
> > 
> > […]
> > 
> > > After all of that, there now exists 2 records in the subscriptions  
> > > table, both of which have the same reader\_id and magazine\_id.  
> > > How do you perform the validation to make sure that each record  
> > > in the subscriptions table has a unique reader\_id, magazine\_id  
> > > pair?
> > 
> > Try this :
> > 
> > class Subscription \< ActiveRecord::Base  
> > belongs\_to :reader  
> > belongs\_to :magazine
> > 
> > validates\_uniqueness\_of :reader\_id, :scope =\> :magazine\_id  
> > end
> > 
> > Good night,
> > 
> > ```
> > -- Jean-FranÃ§ois.
> > 
> > ```
> 
> Thanks Jean-FranÃ§ois!
> 
> Originally I tried putting a validates\_uniqueness\_of in the Subscription  
> model, but I used validates\_uniqueness\_of :reader, :scope =\> :magazine.  
> Of course it didn’t work. And I didn’t think of using the id’s  
> directly, because I was not thinking and didn’t want to put database  
> details in if I didn’t have to. Duh, the model is where those sort of  
> things are supposed to go!
> 
> As I said, thanks again.
