Model relationship design question.

maestro777
2008-05-14T21:33:22Z

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