# Help with relations

**URL:** <https://www.ruby-forum.com/t/help-with-relations/188998>\
**Category:** Rails\
**Created:** [May 21, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-relations/188998 "2010-05-21T16:51:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-relations/188998/1 "2010-05-21T16:51:19Z")

</div>

Hello

I have the three database models: “seller”, “client” and “account”. A  
seller may have many clients and many accounts, but I would like to  
add the constraint that given a seller and a client, only one account  
may exist between them.

Is there a way to express this contraint using ActiveRecord relations?

Thanks in advance,  
Andre

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/help-with-relations/188998/2 "2010-05-21T17:20:43Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hello
> 
> I have the three database models: “seller”, “client” and “account”. A  
> seller may have many clients and many accounts, but I would like to  
> add the constraint that given a seller and a client, only one account  
> may exist between them.
> 
> Is there a way to express this contraint using ActiveRecord relations?

Yes – validates\_uniqueness\_of will do the trick – but never rely  
solely on ActiveRecord constraints. You need a unique index (and  
foreign key constraints) on the DB side too.

> Thanks in advance,  
> Andre

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/help-with-relations/188998/3 "2010-05-21T22:55:28Z")

</div>

On 21 May 2010 15:49, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello
> 
> I have the three database models: “seller”, “client” and “account”. A  
> seller may have many clients and many accounts, but I would like to  
> add the constraint that given a seller and a client, only one account  
> may exist between them.
> 
> Is there a way to express this contraint using ActiveRecord relations?

Does Client belongs\_to seller, and Account belongs\_to seller? If so  
then you could put client and account together in one table.  
Alternatively have Client has\_one account and Account belongs\_to  
client, or the other way round.  
This doesn’t work if you have HABTM relationship of course.

Colin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/help-with-relations/188998/4 "2010-05-21T18:43:08Z")

</div>

On May 21, 12:20 pm, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes – validates\_uniqueness\_of will do the trick – but never rely  
> solely on ActiveRecord constraints. You need a unique index (and  
> foreign key constraints) on the DB side too.

I am already using the validations and key constrains. I was actually  
wondering about the possibility of having Active Record generate  
helper methods reflecting that kind of relation.

Thanks,  
Andre
