我需要知道模型中当前用户的ID:
def after_save
desc, points=nil, nil
if answer_index == daily_question.correct_answer_index
desc = I18n.t('daily_question.point_log.description.correct')
points=daily_question.points
else
desc = I18n.t('daily_question.point_log.description.incorrect')
end
current_user.give_points(:description => desc,
:points => points
)
end
但我想这不是怎么做的?
此致
雅各
答案 0 :(得分:15)
假设用户已登录,您可以使用
UserSession.find.user
您可能需要添加检查以确保UserSession.find
在调用.user
答案 1 :(得分:1)
这是不可能的。
current_user依赖于会话,该会话在模型中不可用(这是正常的。模型与上下文无关)。
您应该将用户作为参数传递给模型。
答案 2 :(得分:0)
使用Authlogic时访问模型中current_user的最佳方法是使用Sentiant用户 - https://github.com/bokmann/sentient_user 有关于如何使用它的完整文档。