If the only difference is the entry in the "sex" field, then I really think creating separate classes is just making things difficult - especially if you want to keep all the data in the same table.
If it were me, I'd create Person methods that return male and female. A simple solution would be something like this:
Code:
class Person < ActiveRecord:Base
def self.find_all_male
find(:all, :conditions => " sex = 'male' ")
end
def self.find_all_female
find(:all, :conditions => " sex = 'female' ")
end
end
Usage would be:
Code:
@men = Person.find_all_male
However, a more flexible approach is to do something like this:
Code:
class Person < ActiveRecord:Base
def self.find_male(options = Hash.new)
new_condition = " sex = 'male' "
find(:all, options_plus_new_condition(options, new_condition)
end
def self.find_female(options = Hash.new)
new_condition = " sex = 'female' "
find(:all, options_plus_new_condition(options, new_condition)
end
private
def self.options_plus_new_condition(options, new_condition)
if options[:conditions]
if options[:conditions].kind_of? Array
options[:conditions][0] << " AND #{new_condition}"
else
options[:conditions] << " AND #{new_condition}"
end
else
options[:conditions] = new_condition
end
return options
end
end
Usage would be
Code:
#get all men
men = Person.find_male
#get all women with blue eyes and sort by name
blue_eyed_women = Person.find_female({:conditions => ["eye_colour = ?", 'blue'], :order => 'name'})
Bookmarks