Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:01:08+00:00 2026-06-11T08:01:08+00:00

I have this custom model, with some methods in it: class GenericModel < ActiveRecord::Base

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:01:10+00:00Added an answer on June 11, 2026 at 8:01 am

    If you want to call the table name used by the subclass, then you’re done – it will do that already:

    class GenericModel < ActiveRecord::Base
      def get_settings
        all # this will get all settings for the subclass that calls it
      end
    end
    

    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:

    class ProvinceSetting < GenericModel
      def special_table_name
        'foo'
      end
    end
    
    class CitySetting < GenericModel
      def special_table_name
        'bar'
      end
    end
    
    class GenericModel < ActiveRecord::Base
      def get_settings
        ... # use 'special_table_name' method in a query
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module like this: module Controller module LocaleModels def self.included(base) base.send :include,
This is my custom model binder. I have my breakpoint set at BindModel but
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I'm currently learning Django and some of my models have custom methods to get
I have implemented a custom Table Model as follows: public class MyTableModel extends AbstractTableModel
I`m trying to display classes that have properties of type some custom class inside
I have this custom JSlider, which will be used in many other forms/windows. But
I have this custom validation attribute for validating a collection. I need to adapt
i have this custom tab widget.. before i've customized it, by default i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.