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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:25:32+00:00 2026-05-24T05:25:32+00:00

I have a common method that exists in my model because it is called

  • 0

I have a common method that exists in my model because it is called by my model. Retrospectively, my view also requires this model method. In order to accomplish this, I have:

  1. moved the model method to the application_helper.rb file
  2. my model calls the application_helper method by adding include ApplicationHelper at the top of my ActiveRecord model

Functionality wise, it works. But is this good practice?

My Model looks like this:

class Client < ActiveRecord::Base
  include ApplicationHelper
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-05-24T05:25:34+00:00Added an answer on May 24, 2026 at 5:25 am

    Writing include ApplicationHelper in to your model is bad practice because ApplicationHelper is a nice place to put tons of helper functions you need in your views. These functions will end up being imported as instance methods of your model. These functions are mostly unrelated to your model and will not work if they depend on things like params or request. Here are two other options:

    Option 1:

    You can just define the method inside the Client class, and then call it from the view, like this:

    class Client < ActiveRecord::Base
        def self.my_class_method
        end
        def my_instance_method
        end
    end
    

    And then in your view:

    <%= Client.my_class_method %>
    <%= @client.my_instance_method %>
    

    Option 2:

    Make a separate module in lib and include it in the places you need it. The file name should match the module name for auto-loading to work.

    In lib/my_module.rb:

    module MyModule
        def my_method
        end
    end
    

    In your model:

    class Client < ActiveRecord::Base
        include MyModule
        def other_method
          my_method
        end
    end
    

    Include the module in ApplicationHelper so it is available to all your views:

    module ApplicationHelper
        include MyModule
    end
    

    Then in your view you can call it easily:

    <%= my_method %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a method for calculating the greatest common divisor of two integers
I have common functionality that I need to access from all screens of my
i have several common elements (components), that will generate some html. it seems my
I have a common comms library that i have written to communicate with our
I have the presumably common problem of having elements that I wish to place
I currently have a Generic method that reads an Value from the database based
In project i have reference to Microsoft.Office.Word.Server and in code i have method that
I have a scenario where I have several classes that extend from a common
I have this scenario, which I think must be pretty common: class Parameter {
Let's say I have a common method which creates a DB connection: Connection getConnection()

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.