Scott Tesoriere
2007-Aug-17 17:54 UTC
new to rails - question about :through and habtm - example
I''ve been designing databases for a few years now, but I''m just recently starting a project, and rails seems like a good candidate to use for it. I think I might be confused about how rails deals with associations, this is the situation: I have a table of people, roles, and classes (also people_roles, people_roles_classes?). People can have many roles, and a particular role can be associated with many people. That seems like a standard HABTM relationship. But then we throw in the classes: an instance of people & roles (people_roles) will be associated with many different classes as different roles (teacher/student), so would the models look like this? class People < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :people end class People_Role < ActiveRecord::Base has_and_belongs_to_many :classes end class Classes has_and_belongs_to_many :people_roles end I think I just confused myself even more, but is this almost correct? -- 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 -~----------~----~----~----~------~----~------~--~---
louismrose-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Aug-17 19:54 UTC
Re: new to rails - question about :through and habtm - example
Hey Scott, I''m new to Rails myself, but hope I can help in some way... Perhaps a simplification to your model might help. Would it possible to consider it this way instead: A person may attend many classes. Additionally, a class may be attended by many people. When a person attends a class, they are associated a role. This simplification prohibits a person from having more than one role for a particular class. For example, Alan can attend Chemistry as a student and English as a teacher. But Alan could not attend Chemistry as a student AND a teacher. Let me know if this simplification seems viable; if so, I''ll see if I can help you build your models :-) Hope this helps. Cheers, Louis. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Scott Tesoriere
2007-Aug-17 21:47 UTC
Re: new to rails - question about :through and habtm - examp
Hey Louis, I''ve been looking about the design this afternoon, and it looks like it would be easier to explicitly spell out the roles (student/teacher/etc) into tables with polymorphic associations. The previous design was from more of a high level to prevent redundancy of data between roles; a little won''t hurt. As to preventing a single person from being duplicated in a class, did you plan on attempting this through the sql by making the id''s unique? Or is there an easier way in the model (.rb) itself? Thanks for your help! -- Scott louismrose-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hey Scott, > > I''m new to Rails myself, but hope I can help in some way... > > Perhaps a simplification to your model might help. Would it possible > to consider it this way instead: > > A person may attend many classes. Additionally, a class may be > attended by many people. When a person attends a class, they are > associated a role. > > This simplification prohibits a person from having more than one role > for a particular class. For example, Alan can attend Chemistry as a > student and English as a teacher. But Alan could not attend Chemistry > as a student AND a teacher. > > Let me know if this simplification seems viable; if so, I''ll see if I > can help you build your models :-) > > Hope this helps. > > Cheers, > Louis.-- 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 -~----------~----~----~----~------~----~------~--~---
louismrose-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Aug-18 08:21 UTC
Re: new to rails - question about :through and habtm - examp
Hey Scott, Great. I''m glad you''ve found a different approach that works! Yes: I guess my thinking is if the IDs are unique, and the resolution table (people_classes) contains a primary composed of the two foreign keys (person_id, class_id), then a single person could not be duplicated in a class (and vice-versa). Good luck! - Louis. On Aug 17, 10:47 pm, Scott Tesoriere <rails-mailing-l...@andreas- s.net> wrote:> Hey Louis, > > I''ve been looking about the design this afternoon, and it looks like it > would be easier to explicitly spell out the roles (student/teacher/etc) > into tables with polymorphic associations. The previous design was from > more of a high level to prevent redundancy of data between roles; a > little won''t hurt. > > As to preventing a single person from being duplicated in a class, did > you plan on attempting this through the sql by making the id''s unique? > Or is there an easier way in the model (.rb) itself? > > Thanks for your help! > > -- Scott > > > > louismr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote: > > Hey Scott, > > > I''m new to Rails myself, but hope I can help in some way... > > > Perhaps a simplification to your model might help. Would it possible > > to consider it this way instead: > > > A person may attend many classes. Additionally, a class may be > > attended by many people. When a person attends a class, they are > > associated a role. > > > This simplification prohibits a person from having more than one role > > for a particular class. For example, Alan can attend Chemistry as a > > student and English as a teacher. But Alan could not attend Chemistry > > as a student AND a teacher. > > > Let me know if this simplification seems viable; if so, I''ll see if I > > can help you build your models :-) > > > Hope this helps. > > > Cheers, > > Louis. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
primary0
2007-Aug-21 12:40 UTC
Re: new to rails - question about :through and habtm - example
I ran into a similar situation recently. The best I could come up with was remodelling the structure (as everyone else in this thread have said) instead of trying to hack around to get it working as it was. But it could be possible to come up with something by redefining classes and maybe writing ugly SQL yourself, although I do not believe that it is the ideal way to do it as far as scalability and (later) readability is concerned. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---