Multiple has_many's

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

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

Hi Fred,

No this is not the case, job_submissions doesn’t have a account_id key.

Rick

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.

Hello Thorsten,

A job can be assigned to multiple accounts.

Rick

Rick,

maybe this is what you’re looking for:

http://agilewebdevelopment.com/plugins/nested_has_many_through

Regards
Philipp