Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common method in model and helper

Tags:

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 
like image 534
Coderama Avatar asked Jul 31 '11 03:07

Coderama


People also ask

What are helper methods in Rails?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.

How do you call a helper model?

Make sure the module file is named properly, meaning in your case application_helper. rb and it's located on the helpers library. You can also try to include the helper in the ApplicationController (app/controller/application_controller. rb).

What is Helper_method in Ruby?

The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers).


2 Answers

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 %> 
like image 174
David Grayson Avatar answered Oct 02 '22 18:10

David Grayson


If you do want to move it to a helper, you should move it in to the client_helper, as it is something just for your Client model and not for the whole application.

The method you speak of though, is it a static class method or an instance method? If it's an instance method, then your models (even if they're in views) can call that method. If it's a static class method, then your views can use it too by calling it like any other static class method (i.e, Client.do_method or something).

I don't see any reason why it needs to be in a helper, unless your method has absoloutely nothing to do with your model, in which case that would be a different question.

like image 20
MrDanA Avatar answered Oct 02 '22 20:10

MrDanA