简体   繁体   中英

Authlogic's current_user object in models

I need to know the ID of the current user in a model:

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

But I guess that is not how it is done?

Regards,

Jacob

assuming the user is loggedin you can use

UserSession.find.user

You might want to add checks to ensure UserSession.find returns something before calling .user

It's not possible.
The current_user relies on the session, which isn't available in the model (and that's normal. The models are context-independent).

You should pass the user to the model as a parameter.

使用Authlogic时访问模型中current_user的最佳方法是使用Sentiant用户 - https://github.com/bokmann/sentient_user有关于如何使用它的完整文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM