I am trying to figure out the has / belongs_to etc relationship for rails but everyone always uses very abstract examples. Could someone fill in the blanks on the following example I came up with over lunch? In my actual program I never bothered filling out these relationships but thought it might be a good idea to fix that. Do you always fill these out and what sort of benifits do you get from it? Say we have a school with students, teachers, classes and books. A teacher can teach 1 to 3 classes A student takes 6 of 10 offered classes Each class has exactly 1 unique book [A student: Takes some but not all classes, Has different teachers, but may have the same teacher multiple times Has one book per class or 6 books] The students could print out their booklist, schedule. The teacher could print out a list of their classes and the students in them. etc. A rough idea for the tables might look like so Teachers id, name..... Student id, name.... Class id, description... schedule id, teacher_id, student_id, class_id Book id, class_id How would the models look? Here is what I came up with Teacher belongs_to :schedule Student belongs_to :schedule Class belongs_to :schedule has_one :book Schedule has_many :teacher has_many :student has_many :class Book belongs_to :class -- 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2006-Oct-19 18:11 UTC
Re: Relationship Advice - who do I belong to & can I have many?
On Thu, 19 Oct 2006, Mic wrote:> > I am trying to figure out the has / belongs_to etc relationship for > rails but everyone always uses very abstract examples. Could someone > fill in the blanks on the following example I came up with over lunch?Not your example, but this will probably help... http://mboffin.com/stuff/ruby-on-rails-data-relationships.png> > In my actual program I never bothered filling out these relationships > but thought it might be a good idea to fix that. Do you always fill > these out and what sort of benifits do you get from it? > > Say we have a school with students, teachers, classes and books. > > A teacher can teach 1 to 3 classes > A student takes 6 of 10 offered classes > Each class has exactly 1 unique book > [A student: > Takes some but not all classes, > Has different teachers, but may have the same teacher multiple times > Has one book per class or 6 books] > > The students could print out their booklist, schedule. The teacher > could print out a list of their classes and the students in them. etc. > > A rough idea for the tables might look like so > > Teachers > id, name..... > > Student > id, name.... > > Class > id, description... > > schedule > id, teacher_id, student_id, class_id > > Book > id, class_id > > > How would the models look? Here is what I came up with > > Teacher > belongs_to :schedule > > Student > belongs_to :schedule > > Class > belongs_to :schedule > has_one :book > > Schedule > has_many :teacher > has_many :student > has_many :class > > Book > belongs_to :class > > -- > 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 -~----------~----~----~----~------~----~------~--~---
That was just the sort of thing I was looking for! Philip Hallstrom wrote:> Not your example, but this will probably help... > > http://mboffin.com/stuff/ruby-on-rails-data-relationships.png-- 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 -~----------~----~----~----~------~----~------~--~---
Chris Mear
2006-Oct-19 22:16 UTC
Re: Relationship Advice - who do I belong to & can I have many?
Mic wrote:> Relationship Advice - who do I belong to & can I have many?First off, I should point out that ''belong to'' is a strong term. In any good relationship, there should certainly be a sense of belonging towards each other, but you shouldn''t ever feel that you merely ''belong to'' somebody, as if you were their property. Otherwise you''ll be dependent on them, and what will happen when they''re gone? With that out of the way, the simple answer is that you belong to your ''significant other'', although I admit that that''s something of a tautology. Do you have someone locally who responds to ''girlfriend'', ''boyfriend'', ''wife'', or ''husband''? ''Honey''? ''Snookie-pie''? If you do, chances are that you belong to them. And to get the full benefits of the relationship, you should always make sure the association is set up in the other direction as well. You should note that in the case of ''wife'' and ''husband'', a slightly more formal API applies: http://en.wikipedia.org/wiki/Marriage_vows As for the question of whether you can have many, the answer is generally no, particularly in the case of the formal API (although, as usual, there are exceptions depending on what environment you''re in). Most people just have one. If you insist on having many, it is possible, and you''ll find that each relationship basically works the same as usual. And you can still keep your original ''has one'' relationship -- that way, you can refer to one of your many (by default, the earliest one) as a special case. Just make sure you don''t confuse their names. And you certainly don''t want to find all of them in the same place. It''s probably best if you keep them within different scopes. Of course, I don''t recommend this kind of chicanery. It is likely to be deprecated soon, anyway. Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Will Gant
2006-Oct-19 22:40 UTC
Re: Relationship Advice - who do I belong to & can I have many?
From my dating experiences, I can also confirm that :has_many from my end results in cache misses. :-) Yeah, I know. It''s lame. Chris Mear <chris-OIzkuoyqg0kAvxtiuMwx3w@public.gmane.org> wrote: Mic wrote:> Relationship Advice - who do I belong to & can I have many?First off, I should point out that ''belong to'' is a strong term. In any good relationship, there should certainly be a sense of belonging towards each other, but you shouldn''t ever feel that you merely ''belong to'' somebody, as if you were their property. Otherwise you''ll be dependent on them, and what will happen when they''re gone? With that out of the way, the simple answer is that you belong to your ''significant other'', although I admit that that''s something of a tautology. Do you have someone locally who responds to ''girlfriend'', ''boyfriend'', ''wife'', or ''husband''? ''Honey''? ''Snookie-pie''? If you do, chances are that you belong to them. And to get the full benefits of the relationship, you should always make sure the association is set up in the other direction as well. You should note that in the case of ''wife'' and ''husband'', a slightly more formal API applies: http://en.wikipedia.org/wiki/Marriage_vows As for the question of whether you can have many, the answer is generally no, particularly in the case of the formal API (although, as usual, there are exceptions depending on what environment you''re in). Most people just have one. If you insist on having many, it is possible, and you''ll find that each relationship basically works the same as usual. And you can still keep your original ''has one'' relationship -- that way, you can refer to one of your many (by default, the earliest one) as a special case. Just make sure you don''t confuse their names. And you certainly don''t want to find all of them in the same place. It''s probably best if you keep them within different scopes. Of course, I don''t recommend this kind of chicanery. It is likely to be deprecated soon, anyway. Chris --------------------------------- Do you Yahoo!? Everyone is raving about the all-new Yahoo! Mail. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Great post Chris! It was just what I need on a Friday! -- 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 -~----------~----~----~----~------~----~------~--~---