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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:04:48+00:00 2026-05-24T01:04:48+00:00

I am using Ruby on Rails 3.0.9 and I am trying to make it

  • 0

I am using Ruby on Rails 3.0.9 and I am trying to make it possible to call some methods from all inside helper, controller, models and view files. What I would like to make is to create a library to build\standardize naming conventions internally to my application so that I can use\run something like the following:

standardized_name = standardize_name(Article)

What do you advice about?

P.S.: I have heard of mixin, but how should I use those in my case? Furthermore I would like not to state inclusion of that mixin in each class that need to use that (that is, I would like to have a “unique” statement that includes the mixin in all my application classes).

  • 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-24T01:04:48+00:00Added an answer on May 24, 2026 at 1:04 am

    Depending on your use case, you can take one of these approaches to rather attach the method to the objects it acts on. Then you don’t need to include the standardize_name method everywhere you want to use it; you just centralized it on the objects to which it applies:

    To use as Article.standardized_name (class method):

    If your standardize_name method only takes ActiveRecord models as arguments, consider putting it in a module (in lib/standardizable.rb for example) and extending it onto ActiveRecord::Base (or some other class that is a parent to all your domain classes):

    module Standardizable
      def standardized_name
        # in here, self will be the model *class*,
        # so the following will return "Article" if you
        # extended the module into the Article class:
        self.name
      end
    end
    
    # include the mixin in all ActiveRecord models:
    ActiveRecord::Base.extend Standardizable
    

    Alternatively, if you don’t want to include it in all models, just include the module into the individual classes where you want standardized_name to be available:

    class Article < ActiveRecord::Base
      extend Standardizable
    end
    

    With both of the following approaches, you will be able to use the method like so:

    standardized_name = Article.standardized_name
    

    To use as Article.new.standardized_name (instance method):

    If you’d rather use the method on instances of your model classes, just change extend in the above example to include:

    module Standardizable
      def standardized_name
        # in here, self will be the model object *instance*,
        # so the following will return "Article" if you
        # included the module into the Article class:
        self.class.name
      end
    end
    
    # include the mixin in all ActiveRecord models:
    ActiveRecord::Base.send(:include, Standardizable)
    

    The usage then looks like this:

    article = Article.new
    
    standardized_name = article.standardized_name
    

    Wrapping up with ActiveSupport::Concern:

    A nice way to wrap up both these approaches in Rails 3 is with ActiveSupport::Concern:

    module Standardizable
      extend ActiveSupport::Concern
    
      module ClassMethods
        def standardized_name
          self.name
        end
      end
    
      module InstanceMethods
        def standardized_name
          self.class.standardized_name
        end
      end
    end
    
    ActiveRecord::Base.extend Standardizable
    

    Then you can use both the class and instance method to get the standardized name:

    standardized_name_from_class = Article.standardized_name
    article = Article.new
    standardized_name_from_instance = article.standardized_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi trying to make a Registration page with Ruby on rails using the tutorial
I am trying to make a auto complete box using ruby on rails, jquery
I'm trying to write an app using Ruby on Rails and I'm trying to
I'm trying to build a web service using Ruby on Rails. Users authenticate themselves
I'm using Ruby on Rails to make an application where businesses can create accounts
I'm using Ruby on Rails with jQuery and trying to do the following: <%=
I am trying to build a free web application using ruby/rails It should be
I was trying to switch the layout using Ruby on Rails but I am
I am trying to set up Ruby on Rails on windows. I am using
I am using Ruby on Rails 3.0.7 and I would like to find some

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.