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?
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 ?
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.
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.