开发者

Rails: Model instance method or helper method?

开发者 https://www.devze.com 2023-03-20 21:30 出处:网络
By convention, should the following be defined as an instance method of my model or a helper method? # app/models/user开发者_开发问答.rb

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.


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


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

This way, you keep coherent logic and tests.

0

精彩评论

暂无评论...
验证码 换一张
取 消