12

按照惯例,应该将以下内容定义为我的模型的实例方法还是辅助方法?

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

或者

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

非常感谢。

4

2 回答 2

9

使用第一个(保留模型),也是因为您可能想做其他事情,例如组合索引:)

于 2011-07-15T23:47:12.473 回答
5

与您的模型直接相关的所有内容都应保留在您的模型中。

这样,您就可以保持连贯的逻辑和测试。

于 2011-07-15T23:44:28.800 回答