Model relationship design question.

I have the following scenario.

Three tables - Companies, Users and Submissions.

Starting with Companies. Each company has many users. Each user can have one or more submissions (of articles related to a company). Hence each submission needs to be linked to a company. Up to this stage, I could probably use :through to link submissions to Companies. So far so good.

But the complication comes in as such: Each user can make a submission related not only to the company he belongs to but also to other companies available on record. In which case the :through option may not work any more in this scenario, I think.

I have been thinking about this for days and due to my limited knowledge of relationships, I couldn't come up with a feasible solution. So far I came up with the following relationships:

class Company < ActiveRecord::Base     has_many :users     has_many :submissions end

class User < ActiveRecord::Base     belongs_to :company     has_many :submisions end

class Submission < ActiveRecord::Base     belongs_to :company     belongs_to :user end

I have a suspicion that this setup will not work. The more I think about it, the more confused I become. If someone could help with some inputs/advice as how to go about this, it will be greatly appreciated.

Thanks in advance. Steve

Company

has_many :users has_many :submissions, :through => :users

User belongs_to :company has_many :submissions

Submission belongs_to :user

has_many :through is awesome.

Thanks Ryan for your input. I thought over you have suggested, and I think it doesn't quite work for the case that I have.

Let me try to explain my scenario again.

I have a table of companies. Each company has multiple users. Each user can submit one or more articles. Each article is about a company (that exists in the company table) and that company may or may not be the same company that the user (submitting the article) belongs to.

For example. I have a User1 who belongs to Company A. He creates an article submission about Company B.

How do I link the submission to Company B, since the article is about company B?. Using the has_many :through => users as you have suggested, I believe it will link the submission to Company A instead.

Maybe the model I am using to approach the problem may not be correct. I am open to learning a better approach to this type of scenario.

Thanks again

Off top, I'm thinking you want to keep your 1:M relationships between companies and users, and between users and submissions, but go M:M on the one between companies & submissions. IOW, make the relationship between submissions & companies independent of the user. So something like:

class User < ar::base   belongs_to :company   has_many :submissions end

class CompaniesSubmissions < ar::base   belongs_to :company   belongs_to :submission end

class Submission < ar::base   belongs_to :user   has_many :companies_submissions   has_many :companies, :through => :companies_submissions end

class Company < ar::base   has_many :users   has_many :companies_submissions   has_many :submissions, :through => :companies_submissions end

You'll probably at least want to default the addition of the submitting user's company to Submission.companies (if not actually enforce that as a validation requirement.) Not sure what the most graceful way to do that would be...

HTH,

-Roy

maestro777 wrote:

Thanks Ryan for your input. I thought over you have suggested, and I think it doesn't quite work for the case that I have.

Let me try to explain my scenario again.

I have a table of companies.

OK

class Company < ActiveRecord::Base end

Each company has multiple users.

class Company < ActiveRecord::Base   has_many :users end

class User < ActiveRecord::Base   belong_to :company end

Each user can submit one or more articles.

class Article < ActiveRecord::Base   belongs_to :user end

class Company < ActiveRecord::Base   has_many :users end

class User < ActiveRecord::Base   belong_to :company   has_many :articles end

Each article is about a company (that exists in the company table) and that company may or may not be the same company that the user (submitting the article) belongs to.

class Article < ActiveRecord::Base   belongs_to :user   belongs_to :company end

class Company < ActiveRecord::Base   has_many :users   has_many :articles end

class User < ActiveRecord::Base   belong_to :company   has_many :articles end

I am not sure you need :through relationships at all.

You don’t need the through relationships. It wasn’t clear to me originally what he wanted, so I assumed he wanted to see what submissions were made by the users of a company, and has_many :through came to mind.

Andrew’s solution seems to fit the bill, I think.

Ryan Bigg wrote:

You don't need the through relationships. It wasn't clear to me originally what he wanted, so I assumed he wanted to see what submissions were made by the users of a company, and has_many :through came to mind.

Andrew's solution seems to fit the bill, I think.

What's more - it's exactly what maestro777 started with.

I suspect he might be having problems getting the results he wants using this structure.

I noticed this too.

Thanks guys for all the input.

I'm glad to find out that I was on the right track after all from the beginning, with my models set up. It wasn't that I was having problem getting the result I wanted. Rather the source of my confusion came from a misunderstanding on what I was reading in one of the ROR books about model relationship. Now that you confirmed I was on the right track, I went back to read the same passage again and realized that it was talking about a different kind of relationship, than what I had in mind.

Thanks again for the help.

One user (who coincidentally happens to belong to a company) writes an article about zero or more companies. You will need, in addition to users, submissions, and companies, a new table (and model) that has all three ids: user_id, submission_id, and company_id.

Or, since a submission is the product of only one user, you put user_id in submissions, and then do a many:many between submissions and companies (so you have a submissions_companies join table w/submission_id and company_id).