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

The Archive Base Latest Questions

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

I have a couple of methods I’d like to share between models rather than

  • 0

I have a couple of methods I’d like to share between models rather than copying and pasting them. In a controller, I can do this by putting the methods in application_controller.rb which automatically gets included in every controller.
Is there a similar thing for models?

Thanks!

  • 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-13T22:33:03+00:00Added an answer on May 13, 2026 at 10:33 pm

    You can create a file called functionality.rb or something similar in the lib directory of your Rails app. Make the file named after the module to autoload it when Rails starts. For example, if I wanted to add flagging to multiple models, I’d create a file called lib/flagging.rb, and it would look like this:

    module Flagging
      # Flags an object for further review
      def flag!
        self.update_attribute(:flagged, true)
      end
    
      # Clears all flags on an object
      def deflag!
        self.update_attribute(:flagged, false)
      end
    end
    

    In every model I wanted to add this functionality to, I’d do the following:

    class Foo < ActiveRecord::Base
      include Flagging
    end
    

    I’d then be able to do something like:

    foo = Foo.create
    foo.flag!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do have couple of solutions(rather call them hacks) for doing this but looking
In appllication controller i have couple of methods that works with requested controller and
I have a couple of methods that check for assignablity or interface assignabilty between
I have a couple of simple models that are associated like so: MODELS class
I have a controller with a couple of methods and they all have the
I have created a web service which has a couple of methods developed using
I have a abstract base C# class with a couple of methods that has
We have couple different web-apps that share the same db link. The hibernate layer
I have a couple of methods declared at the namespace level within a header
I have a couple extension methods that handle serialization of my classes, and since

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.