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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:02:40+00:00 2026-05-29T22:02:40+00:00

I understand that application_controller.rb is the place to put all the methods, etc that

  • 0

I understand that application_controller.rb is the place to put all the methods, etc that you would like made available in all your controllers since they all inherit from this class. Great.

But what is the equivalent for Models? In other words, I want a place where I can create a couple of super classes that my models will inherit from.

For example, I have a method that searches different tables for entries in all CAPS via REGEXP in Mysql. I’d like to be able to create the method only once and call it for different tables/models.

What is the Rails way of doing this?

I thought I could create a class that would inherit from ActiveRecord::Base (as all models do) , put the methods in there and then inherit all my models from that class. But thought there would surely be a better way to do it.

Thanks.

Edit

Per Semyon’s answer I’m editing the post to show the routes I am using. It works now:

# models/dvd.rb
require 'ModelFunctions'
class Dvd < ActiveRecord::Base
    extend ModelFunctions
    ...
end

# lib/ModelFunctions.rb
module ModelFunctions
    def detect_uppercase(object)
        case object
        ...
        where("(#{field} COLLATE utf8_bin) REGEXP '^[\w[:upper:]]{5,}' ").not_locked.reorder("LENGTH(#{field}), #{table}.#{field} ASC")
    end
end

In config/application.rb

config.autoload_paths += %W(#{config.root}/lib)
  • 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-05-29T22:02:41+00:00Added an answer on May 29, 2026 at 10:02 pm

    Put the reusable method in some module next to your Dvd class. You can move it in a separate file later.

    # app/models/dvd.rb
    module CaseInsensitiveSearch
      def case_insensitive_search(field, value)
        # searching field for value goes here
      end
    end
    
    class Dvd
    end
    

    After extending a class with the module you can use case_insensitive_search on the class. Including the module will make case_insensitive_search an instance method which is not what you want.

    class Dvd
      extend CaseInsensitiveSearch
    end
    
    Dvd.case_insensitive_search("title", "foo")
    

    And of course you can use it inside Dvd class.

    class Dvd
      def self.search(query)
        case_insensitive_search("title", query)
      end
    end
    
    Dvd.search("foo")
    

    Now when you made sure it works, you will probably want to move it in a separate file and use it across multiple classes. Place it in lib/case_insensitive_search.rb and make sure you have this line in config/application.rb:

    config.autoload_paths += %W(#{config.root}/lib)
    

    Now you can require it anywhere you want to use it:

    require 'case_insensitive_search'
    
    class Dvd
      extend CaseInsensitiveSearch
    end
    

    The last thing I’d like to suggest. Create multiple modules with meaningful names. So instead of CommonModel have CaseInsensitiveSearch and so on.

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

Sidebar

Related Questions

I understand that a ModelBinder is a good place to do work on the
I have recently designed a web application that I would like to write in
I would like to create an utility application that has a navigation based flipside,
I understand that the application_controller will produce variable values for an entire application but
I understand that view controllers help control multiple views in an application, but I
I understand that creating too many threads in an application isn't being what you
i understand that some files get published to this folder in my application. i
I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile
Ignoring the IE case, are there any other browsers that can't understand the application/xhtml+xml
I understand that some countries have laws regarding website accessibility. In general, what are

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.