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 7744547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:47:38+00:00 2026-06-01T09:47:38+00:00

I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method

  • 0

I have a standard ActiveRecord model with the following:

class MyModel < ActiveRecord::Base
  custom_method :first_field, :second_field
end

At the moment, that custom_method is picked up by a module sent to ActiveRecord::Base. The functionality basically works, but of course, it attaches itself to every model class, not just MyModel. So if I have MyModel and MyOtherModel in the same action, it’ll assume MyOtherModel has custom_method :first_field, :second_field as well.

So, my question is: How do I attach a method (eg: def custom_method(*args)) to every class that inherits from ActiveRecord::Base, but not by attaching it to ActiveRecord::Base itself?

Any ideas appreciated.

===

Edit

The custom_method is currently attached to ActiveRecord::Base by the following:

module MyCustomModule
  def self.included(base)
    base.extend(self)
  end

  def custom_method(*args)
    # Zippity doo dah - code goes here
  end
end

ActiveRecord::Base.send(:include, MyCustomModule)
  • 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-01T09:47:39+00:00Added an answer on June 1, 2026 at 9:47 am

    Do you know about descendants?

    ActiveRecord::Base.descendants
    

    You have to be sure to touch the models before calling it.

    See excellent discussion here:

    Is there a way to get a collection of all the Models in your Rails app?

    I concur with the commentors above that you may want to consider adding your methods to the meta class, or an intermediary class, or a Module mixin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have a standard active record model with an attributes that is required: class
I have a standard relationship: class User <ActiveRecord:: Base has_and_belongs_to_many: feeds,: uniq => true
I have a class that looks something like this: class User < ActiveRecord:Base has_many
I have a standard windows server that inherits from the ServiceBase class. On the
I have a has_many :through relationship set up like so class Situation < ActiveRecord::Base
Many languages have standard repositories where people donate useful libraries that they want others
I just realised that standard ActiveRecord actually hits the database when you do person
Is it possible to implement, in Rails 3, an association class ApiCredentials < ActiveRecord::Base
I have a standard Rails app that uses a database, sqlite3 and the webrick

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.