我有一个模型,它看起来是这样的:Rails:将模型添加到模型以基于current_user执行检查?
class Comment < ActiveRecord::Base
...
#allow editing comment if it is moderated and the user passed-in
#is the one that owns the comment
def can_edit?(user)
moderated? and user.Type == User and user.id == self.user_id
end
...
end
并在View的电话:
<%= link_to 'Show Comment', @comment if @comment.can_show?(current_user) %>
我需要写在许多不同的车型很多这样的方法 - 排序验证检查来看看是否允许current_user 在模型上做些什么。
但它感觉很累赘 - 特别是需要检查传入的user
确实是User
类型的对象。
什么是干净的,最好的做法,做这种事情?我在正确的轨道上吗? (即我应该加入这种方法的模型或其他地方)
注意
我使用范围的查询来获得的评论和其他车型,但在某些情况下,我不能范围查询所以我必须使用
can_xxxx?
方法
Ps。我在做什么被认为是“胖胖的模特”?
感谢*很多*。这种方法感觉更清洁。我想现在对于你的技术有很多深入的理解! – Zabba 2010-10-21 15:34:23