Re: Model relationship design question.

Pardee, Roy
2008-05-15T00:26:48Z

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

-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
maestro777
Sent: Wednesday, May 14, 2008 2:33 PM
To: Ruby on Rails: Talk
Subject: [Rails] 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




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com