用户模型: has_many :课程
课程模型: belongs_to :用户
def require_course
unless #check if current user has course
redirect_to root_url
return false
end
end 我需要一个方法,检查当前用户是否有课程。我应该写什么来检查current_user是否有课程。
发布于 2010-12-06 04:11:29
我会去找
def require_course
redirect_to root_path if @user.courses.blank?
endDocumentation about Object#blank?
发布于 2010-12-06 03:06:36
那current_user.courses.size > 0呢?
发布于 2010-12-06 03:11:01
甚至是更短的一个:
redirect_to(root_url) if @user.courses.size.zero?https://stackoverflow.com/questions/4360485
复制相似问题