# What should e relationship etween these models

**URL:** <https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064>\
**Category:** Rails\
**Created:** [January 22, 2010, 7:14am UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064 "2010-01-22T07:14:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 22, 2010, 7:14am UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/1 "2010-01-22T07:14:05Z")

</div>

Hi

I have a organizations table and employess table. From the employee one  
employee will e administering the whole organizations That means  
creating other staff and what ever the activities etc. My question is  
how can I implement the table structure and set relation for  
organizations and employees table I started like

organizations has\_many employees  
employee belongs\_to organization

```
 But how should I represent an employee of organization who is the

```

administrator

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 22, 2010, 11:12am UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/2 "2010-01-22T11:12:12Z")

</div>

Consider this example:

> organizations has\_many employees  
> employee belongs\_to organization  
> employee belongs\_to adminstered\_organization

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 22, 2010, 11:23am UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/3 "2010-01-22T11:23:03Z")

</div>

Hi

> Consider this example:
> 
> > organizations has\_many employees  
> > employee belongs\_to organization  
> > employee belongs\_to adminstered\_organization

Could you please specify the model structure and relation now?

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 22, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/4 "2010-01-22T14:16:33Z")

</div>

Tom M. wrote:

> Hi
> 
> > Consider this example:
> > 
> > > organizations has\_many employees  
> > > employee belongs\_to organization  
> > > employee belongs\_to adminstered\_organization
> 
> Could you please specify the model structure and relation now?

maybe the requirement needs to be expanded a touch as it was a little  
unclear to me on what this ‘administrator’ does. Are you modelling a  
business role or a system role (and/or are they the same thing?). Are  
there other roles you may need to model as this would potentially change  
the way you implement the behaviour i.e. the introduction of a roles  
model.

PS I found that a good guide to associations which helped me was

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 25, 2010, 12:28pm UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/5 "2010-01-25T12:28:49Z")

</div>

Tom M. wrote:

> Hi Jon C.
> 
> Thanks for your reply. My idea is to create a saas based application  
> which has the initial signup procedure in basecamp like
> 
> [Basecamp Classic Signup](https://signup.37signals.com/basecamp/Basic/signup/new?source=signin-screen& __utma=1.193660493.1263811484.1263811484.1264389803.2&__ utmb=1.10.10.1264389803& __utmc=1&__ utmx=-&__utmz=1.1263811487.1.2.utmcsr=google%7Cutmccn=(organic)%7Cutmcmd=organic%7Cutmctr=basecamp&__utmv=-&__utmk=101930241)
> 
> So as a first step I tried to design tables organizations and employees  
> as above and putting organization\_owner\_id in in organization table  
> .Please correct me if I am wrong
> 
> Thanks  
> Tom

For something like Basecamp you’ll end up with users having different  
types of role. Things like administrators, project managers, project  
leads, project members etc. I presume that a user could have more than  
one role as well? This lends itself to a structure to link your users to  
roles (or say employees to orangisation\_roles) perhaps along the lines  
of:

class Employee \< ActiveRecord::Base  
has\_many :employee\_roles  
has\_many :roles, :through =\> :employee\_roles  
end

class Role \< ActiveRecord::Base  
has\_many :employee\_roles  
has\_many :employees, :through =\> :employee\_roles  
end

class EmployeeRole \< ActiveRecord::Base  
belongs\_to :role  
belongs\_to :employee  
end

One of the roles could be the organisation owner as per your initial  
requirement.

Again - i’d recommend reading the guide on assosiations  
([Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html)). more  
importantly though is to have a clear design to build against. I’ve  
recently been looking into Behaviour Driven Design (BDD) and it’s  
already changing the way i’m approaching my work. Work through the  
behaviours you need your app to deliver before you work on a solution 🙂

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 25, 2010, 4:28am UTC](https://www.ruby-forum.com/t/what-should-e-relationship-etween-these-models/182064/6 "2010-01-25T04:28:42Z")

</div>

Hi Jon C.

Thanks for your reply. My idea is to create a saas based application  
which has the initial signup procedure in basecamp like

[https://signup.37signals.com/basecamp/Basic/signup/new?source=signin-screen&\_\_utma=1.193660493.1263811484.1263811484.1264389803.2&\_\_utmb=1.10.10.1264389803&\_\_utmc=1&\_\_utmx=-&\_\_utmz=1.1263811487.1.2.utmcsr=google](https://signup.37signals.com/basecamp/Basic/signup/new?source=signin-screen& __utma=1.193660493.1263811484.1263811484.1264389803.2&__ utmb=1.10.10.1264389803& __utmc=1&__ utmx=-&__utmz=1.1263811487.1.2.utmcsr=google%7Cutmccn=(organic)%7Cutmcmd=organic%7Cutmctr=basecamp&__utmv=-&__utmk=101930241)

So as a first step I tried to design tables organizations and employees  
as above and putting organization\_owner\_id in in organization table  
.Please correct me if I am wrong

Thanks  
Tom
