# Modeling 2 relationships between 2 entites

**URL:** <https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686>\
**Category:** Rails\
**Created:** [December 10, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686 "2007-12-10T19:21:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 10, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686/1 "2007-12-10T19:21:42Z")

</div>

Hello,  
I’ve got the following quiet common domain model.

[http://55degrees.co.uk/ewan/peopleUserGroups.pdf](http://55degrees.co.uk/ewan/peopleUserGroups.pdf)

As you can see a person can create 0 or many groups, a group being  
created by 1 person. A person belongs to 1 or many groups and a group  
can contain many people. A can also contain many sub groups.

A simple person class to describe these relationship would be…

class Person \< ActiveRecord::Base  
#relationships  
has\_many :usergroups  
has\_and\_belongs\_to\_many :usergroups  
end

obviously this is wrong I can’t name both usergroups ‘usergroups’. How  
do I rename the relationship so I can refer to the two relationships.  
Cheers,  
Ewan

---

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 10, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686/2 "2007-12-10T19:45:37Z")

</div>

ewan wrote:

> Hello,  
> I’ve got the following quiet common domain model.
> 
> [http://55degrees.co.uk/ewan/peopleUserGroups.pdf](http://55degrees.co.uk/ewan/peopleUserGroups.pdf)
> 
> As you can see a person can create 0 or many groups, a group being  
> created by 1 person. A person belongs to 1 or many groups and a group  
> can contain many people. A can also contain many sub groups.
> 
> A simple person class to describe these relationship would be…
> 
> class Person \< ActiveRecord::Base  
> #relationships  
> has\_many :usergroups  
> has\_and\_belongs\_to\_many :usergroups  
> end
> 
> obviously this is wrong I can’t name both usergroups ‘usergroups’. How  
> do I rename the relationship so I can refer to the two relationships.

You can name the associations whatever you want, then use the  
:class\_name option to override the implied class name.

class Person \< ActiveRecord::Base  
has\_many :created\_groups, :class\_name =\> “UserGroup”  
has\_and\_belongs\_to\_many :user\_groups  
end

class UserGroup \< ActiveRecord::Base  
belongs\_to :creator, :class\_name =\> “Person”  
has\_and\_belongs\_to\_many :people  
end

Give your user\_groups table a creator\_id field, and you should be good  
to go.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 13, 2007, 2:51am UTC](https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686/3 "2007-12-13T02:51:54Z")

</div>

On 10 Dec 2007, at 17:34, ewan wrote:

> A simple person class to describe these relationship would be…
> 
> class Person \< ActiveRecord::Base  
> #relationships  
> has\_many :usergroups  
> has\_and\_belongs\_to\_many :usergroups  
> end

class Person  
has\_many :created\_groups, :class\_name =\> ‘Usergroup’, :foreign\_key  
=\> ‘person\_id’  
has\_and\_belongs\_to\_many :usergroups  
end  
should do the trick

Fred

---

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 13, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/modeling-2-relationships-between-2-entites/122686/4 "2007-12-13T14:35:32Z")

</div>

Thank you both for your reply and thanks for not just telling me to  
RTFM. Having just started using Ruby and Rails it’s appreciated having  
people how are willing to help with the simple questions.  
Cheers,  
Ewan

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001079)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
