Rails: 模型实例方法还是帮助方法?

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

非常感谢。


这个问题的更一般/无代码示例版本:https://dev59.com/rG445IYBdhLWcg3wIG19 - Ciro Santilli OurBigBook.com
在我看来,最好的选择是使用Presenter/Decorator。 - Ilya
2个回答

9

选择第一种方法(保持模型),这也是因为您可能想要做其他的事情,比如组合索引 :)


5

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

这样,您可以保持一致的逻辑和测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接