Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Model instance method or helper method?

By convention, should the following be defined as an instance method of my model or a helper method?

# app/models/user.rb
class User < ActiveRecord::Base
  def full_name
    "#{first_name} #{last_name}"
  end
end

or

# app/helpers/users_helper.rb
module UsersHelper
  def full_name
    "#{@user.first_name} #{@user.last_name}"
  end
end

Many thanks.

like image 744
gjb Avatar asked Jul 15 '11 23:07

gjb


2 Answers

Go with the first (keep in model), also because you might want to do other stuff, like combined indexes :)

like image 127
kain Avatar answered Oct 11 '22 12:10

kain


Everything directly related to your model should stay in your model.

This way, you keep coherent logic and tests.

like image 5
apneadiving Avatar answered Oct 11 '22 12:10

apneadiving