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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I noticed this too. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On May 14, 11:40 pm, maestro777 <isig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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). -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Larry Sent: Thursday, May 15, 2008 1:10 PM To: Ruby on Rails: Talk Subject: [Rails] Re: Model relationship design question. 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. On May 14, 11:40 pm, maestro777 <isig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Skegg wrote:>> 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.... +1 to Andrew''s suggestion. And let me explain why i think you are confused: Your original suggestion mirrors that of Andrew. I''ve drawn it up in a diagram: Company 1---n User (Relation1) User 1----n Article (Relation2) Company 1---n Article (Relation3) The confusion might come because you think, that this design implies this rule: Company CA has a user U1 U1 writes the article Ai Then you automatically believe, that company CA is related to the article Ai. And it is, but can actually be related in TWO different was: Company CA "owns" (has authored) article Ai (through Relation1 & Relation2) but at the same article Ai can relate to company CA (as the article''s subject) or relate to an entirely different company - through Relation3. Sometimes (probably a lot of the times) the owner of the article and the subject of the article will be the same company. Therefore you believe, that this relation structure is redundant. It is. But at the same time it is a "necessary evil" to accomplice what you want. I hope this clarifies some things. Your relation structure is sound, but you probably didn''t know why. :-) - Carsten -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---