# Join Relationship Model question

**URL:** <https://www.ruby-forum.com/t/join-relationship-model-question/105613>\
**Category:** Rails\
**Created:** [May 31, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/join-relationship-model-question/105613 "2007-05-31T18:23:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![visaolive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/57b2e6/32.png) [@visaolive](https://www.ruby-forum.com/u/visaolive)\
**Post date:** [May 31, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/join-relationship-model-question/105613/1 "2007-05-31T18:23:40Z")

</div>

Hi! I’m modeling my application and i have a question about  
relationships:

I have 3 classes (tables): User (users), Company (companies) and Rol  
(rols)

The relationship between the 3 classes is another table that has:

Joining table X  
user\_id  
company\_id  
rol\_id

Because a the same user can be in many companies. A company can have  
many users. An a user have a single rol in a company but different  
rols in another company.

So i can’t name my table like users\_companies because i have another  
field, the rol\_id so i should find another name, lets say X.

My model should be something like?:

class User \< ActiveRecord::Base  
has\_many :X  
has\_many :companies, :through =\> X  
end

class Company \< ActiveRecord::Base  
has\_many :X  
has\_many :users, :through =\> X  
end

class X \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :users  
has\_one :rol  
end

And rol isn’t a class because just have the type of user (“Admin”,  
“Regular”).

Is this ok? Is my first time doing this kind of relationship ☹

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![visaolive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/57b2e6/32.png) [@visaolive](https://www.ruby-forum.com/u/visaolive)\
**Post date:** [June 1, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/join-relationship-model-question/105613/2 "2007-06-01T13:05:45Z")

</div>

let me first clear something up:  
is “rols” (yopu mean “roles”?) a real table? you wrote so. then why  
shouldn’t it have a model-class? it should!

now about the association:  
#tables:  
users  
companies  
roles  
jobs

class User \< ActiveRecord::Base  
has\_many :jobs  
has\_many :companies, :through =\> :jobs  
has\_many :roles, through =\> :jobs  
end

class Company \< ActiveRecord::Base  
has\_many :jobs  
has\_many :users, :through =\> :jobs  
has\_many :companies, :though =\> :jobs  
end  
class Role \< ActiveRecord::Base  
has\_many :jobs  
has\_many :users, :through =\> :jobs  
has\_many :companies, :through =\> :jobs  
end

You can remove some of the associations when you don’t need them (e.g.  
i dont see a use to find all companies that have a role “Admin”)

you can then do:  
#controller  
@user = User.find\_by\_id(1)  
@jobs = @user.jobs.find :all, :include =\> :companies, :roles

#view  
\<% @jobs.each do |job| %\>

# Jobs of \<%= @user.name %\>

Company: \<%= job.company.name %\>  
 Role:\<%= job.role.name %\>

\<% end %\>

etc. pp.

---

<div class="post-metadata">

**Author:** ![visaolive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/57b2e6/32.png) [@visaolive](https://www.ruby-forum.com/u/visaolive)\
**Post date:** [June 1, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/join-relationship-model-question/105613/3 "2007-06-01T14:45:59Z")

</div>

On 6/1/07, Thorsten [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> let me first clear something up:  
> is “rols” (yopu mean “roles”?) a real table? you wrote so. then why  
> shouldn’t it have a model-class? it should!

Yes, i realized that after sending the email.

> has\_many :companies, :through =\> :jobs  
> has\_many :users, :through =\> :jobs  
> has\_many :companies, :through =\> :jobs  
> end

I did something like that: classes: Role, User, Company and Profile (or  
job).

Where Profile belongs\_to user, company and role.  
And User and Company have relationships like the one you wrote.

> # Jobs of \<%= @user.name %\>
> 
> Company: \<%= job.company.name %\>  
> Role:\<%= job.role.name %\>
> 
> \<% end %\>
> 
> etc. pp.

Thank you very much for your time and helping me understand this a bit  
more.
