# Overriding object instantiation?

**URL:** <https://www.ruby-forum.com/t/overriding-object-instantiation/194971>\
**Category:** Rails\
**Created:** [September 14, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971 "2010-09-14T16:19:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moonshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@moonshadow](https://www.ruby-forum.com/u/moonshadow)\
**Post date:** [September 14, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971/1 "2010-09-14T16:19:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![moonshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@moonshadow](https://www.ruby-forum.com/u/moonshadow)\
**Post date:** [September 14, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971/2 "2010-09-14T16:22:59Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![moonshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@moonshadow](https://www.ruby-forum.com/u/moonshadow)\
**Post date:** [September 14, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971/3 "2010-09-14T16:33:55Z")

</div>

On Sep 14, 11:31 am, moonshadow [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![moonshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@moonshadow](https://www.ruby-forum.com/u/moonshadow)\
**Post date:** [September 15, 2010, 6:01am UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971/4 "2010-09-15T06:01:07Z")

</div>

moonshadow wrote:

> def self.new  
> gender\_class = self.gender == ‘Female’ ? ‘Female’ : ‘Male’  
> eval(gender\_class).new  
> end

Really, 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

---

<div class="post-metadata">

**Author:** ![moonshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@moonshadow](https://www.ruby-forum.com/u/moonshadow)\
**Post date:** [September 15, 2010, 5:44am UTC](https://www.ruby-forum.com/t/overriding-object-instantiation/194971/5 "2010-09-15T05:44:07Z")

</div>

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 C. \<
