Hi all, ROR newbie coming from a PHP background here. I have a model that inherits from ActiveRecord::Base. I would like to extend that into two further models, to separate functionality from each other. Basically: class Person < ActiveRecord::Base attr_accessible :gender end class Male < Person end class Female < Person end How can I write a method within Person that will detect what gender has been defined and return the proper object based on it? I''m thinking something like... def self.new gender_class = self.gender == ''Female'' ? ''Female'' : ''Male'' eval(gender_class).new end In that version, though, "self.gender" isn''t recognized. I''m sure I''m missing something really basic, but can someone point out what it is? Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 14, 11:31 am, moonshadow <equestrian...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How can I write a method within Person that will detect what gender > has been defined and return the proper object based on it? > > I''m thinking something like... > > def self.new > gender_class = self.gender == ''Female'' ? ''Female'' : ''Male'' > eval(gender_class).new > end > > In that version, though, "self.gender" isn''t recognized. I''m sure I''m > missing something really basic, but can someone point out what it is? >new is a class method, but gender is an instance method. Apart from that I''m not sure what you''re trying to do? Do you know that Active Record can handle STI for you ? 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
moonshadow wrote:> Hi all, > > ROR newbie coming from a PHP background here. > > I have a model that inherits from ActiveRecord::Base. I would like to > extend that into two further models, to separate functionality from > each other. Basically: > > class Person < ActiveRecord::Base > attr_accessible :gender > end > > class Male < Person > > end > > class Female < Person > > end > > How can I write a method within Person that will detect what gender > has been defined and return the proper object based on it?You probably want to use single-table inheritance (STI) for this. Read the ActiveRecord docs on this point. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
if you put a field called type in the table of the base class AR will automaticly save to the correct class, in fact from that moment on everything will behave as is you had 2 models, no need for more configuration. On Tue, Sep 14, 2010 at 10:20 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Sep 14, 11:31 am, moonshadow <equestrian...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > How can I write a method within Person that will detect what gender > > has been defined and return the proper object based on it? > > > > I''m thinking something like... > > > > def self.new > > gender_class = self.gender == ''Female'' ? ''Female'' : ''Male'' > > eval(gender_class).new > > end > > > > In that version, though, "self.gender" isn''t recognized. I''m sure I''m > > missing something really basic, but can someone point out what it is? > > > > new is a class method, but gender is an instance method. Apart from > that I''m not sure what you''re trying to do? Do you know that Active > Record can handle STI for you ? > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
moonshadow wrote:> def self.new > gender_class = self.gender == ''Female'' ? ''Female'' : ''Male'' > eval(gender_class).new > endReally, look into how ActiveRecord handles STI by using the type column. It will handle all of this for you. That said, the way to do something when an object is created is with: def initialize # do some stuff, and you should probably call super super end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.