# Multiple has\_many's

**URL:** <https://www.ruby-forum.com/t/multiple-has-manys/134720>\
**Category:** Rails\
**Created:** [April 14, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720 "2008-04-14T13:33:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/1 "2008-04-14T13:33:51Z")

</div>

Hello,

I have the following database structure:

accounts  
account\_jobs (account\_id, job\_id)  
jobs  
job\_submissions (job\_id, submission\_id)  
submissions

My Account model looks like this:

- has\_many :account\_jobs
- has\_many :jobs, :through =\> :account\_jobs

I want to have all the job\_submissions and submissions that belong to  
this account, WITH the ability to further search through them (example:  
Account.find(1).submissions.find(1) ).

Thanks in advance,

Rick

---

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/2 "2008-04-14T13:48:13Z")

</div>

On 14 Apr 2008, at 12:33, Rick Rr wrote:

> My Account model looks like this:
> 
> - has\_many :account\_jobs
> - has\_many :jobs, :through =\> :account\_jobs
> 
> I want to have all the job\_submissions and submissions that belong to  
> this account, WITH the ability to further search through them  
> (example:  
> Account.find(1).submissions.find(1) ).

don’t you just want to add has\_many job\_submissions & has\_many  
submissions :through =\> job\_submissions ?

Fred

---

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/3 "2008-04-14T13:55:46Z")

</div>

Hi Fred,

No this is not the case, job\_submissions doesn’t have a account\_id key.

Rick

---

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/4 "2008-04-14T14:01:41Z")

</div>

well it might be a good idea to add one, otherwise you will haver to  
do a JOIN over 5 tables … with or without a rails-built-in way of  
doing that join, it’s not a very good thing, from a performance  
perspective.

If those submissions always (in terms of logic) belong to to current  
user/account that is creating them, it’s a rather small change to add  
those account\_id columns to the sub\_submissions table, add the  
belongs\_to and add the account in the create action of the  
job\_submission.

---

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/5 "2008-04-14T14:04:54Z")

</div>

Hello Thorsten,

A job can be assigned to multiple accounts.

Rick

---

<div class="post-metadata">

**Author:** ![rick\_531](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rick\_531](https://www.ruby-forum.com/u/rick_531)\
**Post date:** [April 14, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/multiple-has-manys/134720/6 "2008-04-14T21:54:43Z")

</div>

Rick,

maybe this is what you’re looking for:

[http://agilewebdevelopment.com/plugins/nested\_has\_many\_through](http://agilewebdevelopment.com/plugins/nested_has_many_through)

Regards  
Philipp
