# Multi-user multi-company design

**URL:** <https://www.ruby-forum.com/t/multi-user-multi-company-design/134523>\
**Category:** Rails\
**Created:** [April 11, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/multi-user-multi-company-design/134523 "2008-04-11T18:02:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [April 11, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/multi-user-multi-company-design/134523/1 "2008-04-11T18:02:22Z")

</div>

I am working on a site which tracks “ferms” (read: widgets)… each  
ferm belongs to one company, one company can have many ferms.

One company can also have many users with different access priveliges,  
and company administrators would invite other users to access the  
company’s site.

Also, one user can belong to different companies and perhaps have  
different access at each company.

I’m struggling to determine how to nest the models, here is my best  
guess:

User  
has\_many :companies, :through =\> :companies\_users  
end

Company  
has\_many :ferms  
has\_many :users, :through =\> :companies\_users  
end

The companies\_users join table would have a column that describes the  
access (1,2,3?) for that user at that company.

When a user logs in and wants to view his list of ferms at Company X  
the Ferms#index controller would include

@ferms = Ferm.find(:all, :conditions =\> [“company = ?”, User.company])

Am I on the right track with this? Any help is appreciated.

SH

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [April 11, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/multi-user-multi-company-design/134523/2 "2008-04-11T18:24:45Z")

</div>

On 11 Apr 2008, at 17:01, shenry wrote:

> end

You sought of want nested has\_many :throughs, at which point it would  
become obvious.  
Rails doesn’t have that baked in, but there is a plugin (just google  
for nested has\_many through)

Fred
