Let''s say i have a class with a lot of functionality, called Person. It happens that men and women are treated differently within my app and i''m sick of passing :condition => ["gender = ? ", F] all the time for cases dealing with women. Instead, i want to make two new classes, Man and Woman, which inherit all the functionality of Person but only some of the records, based on the above condition. Can anyone tell me the syntax for this please? I know how to extend a class but not to limit it in this way. thanks max -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-05 14:26 UTC
Re: Split a class by extending it, with a condition
On 5 Dec 2007, at 14:18, Max Williams wrote:> > Let''s say i have a class with a lot of functionality, called > Person. It > happens that men and women are treated differently within my app and > i''m > sick of passing > > :condition => ["gender = ? ", F] > > all the time for cases dealing with women. Instead, i want to make > two > new classes, Man and Woman, which inherit all the functionality of > Person but only some of the records, based on the above condition. > > Can anyone tell me the syntax for this please? I know how to extend a > class but not to limit it in this way.Sounds like you want single table inheritance: Keep your existing Person class, with all the common code, and add a string column called ''type'' to the table Now create classes Man and Woman that inherit from Person. Now whenever you do somethine like Man.find :all, or has_many :women etc... it will be scoped to Man or Woman as appropriate. You can still go Person.find :all to get everyone. If you already have existing data, you''ll need to populate the newly created type column with the name of the appropriate subclass of Person. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 5 Dec 2007, at 14:18, Max Williams wrote: > >> two >> new classes, Man and Woman, which inherit all the functionality of >> Person but only some of the records, based on the above condition. >> >> Can anyone tell me the syntax for this please? I know how to extend a >> class but not to limit it in this way. > > > Sounds like you want single table inheritance: > Keep your existing Person class, with all the common code, and add a > string column called ''type'' to the table > Now create classes Man and Woman that inherit from Person. > > Now whenever you do somethine like Man.find :all, or has_many :women > etc... it will be scoped to Man or Woman as appropriate. You can still > go Person.find :all to get everyone. > > If you already have existing data, you''ll need to populate the newly > created type column with the name of the appropriate subclass of Person. > > FredAh, "single table inheritance" - i''d wondered before what that was, now i know the answer :) Perfect, thanks! -- 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 -~----------~----~----~----~------~----~------~--~---