I have this custom model, with some methods in it:
class GenericModel < ActiveRecord::Base
self.abstract_class = true
def get_settings
.....
end
end
I want to inherit this GenericModel class in my various other Models like below, so that I can have access to those custom methods:
class ProvinceSetting < GenericModel
end
class CitySetting < GenericModel
end
But, how do I write my custom method so that it acts on a specific table in the database depending upon which Model class is calling it ? Any pointers would be welcome
Is this the correct implementation of get_settings in GenericModel, where self would refer to appropriate model name ?
def self.get_settings(user_id)
user_session = SettingsSession.find_by_user_id(user_id)
if !user_session.blank?
if setting_orig = self.find_by_settings_session_id(user_session.id)
setting = setting_orig.dup
end
else
setting = self.first.dup
end
return setting
end
If you want to call the table name used by the subclass, then you’re done – it will do that already:
If you need to use some other table name, make a method in each subclass that defines the tablename, and call that method in the abstract base model: