2013-03-28 33 views
0

我想用当前用户的employee_id更新表Tasks中的inspector_id。但是,该模型不会让我访问current_user。rails model before_update无法访问current_user

这是我试图在任务模型中使用的代码。

before_update :update_inspector 

protected 
def update_inspector 
    self.inspector_id = current_user.employee 
end 

我得到:

NameError (undefined local variable or method `current_user' 

回答

1

通常CURRENT_USER在应用控制器中定义。这意味着该方法仅适用于控制器内部,不适用于模型。你所看到的错误与事态是一致的。如果你需要任务模型中的current_user,你必须在某个地方传递它。

0

您可以随时访问在模型内部构建current_user变量的会话信息。

相关问题