Authlogic's current_user object in models

只谈情不闲聊 提交于 2020-01-22 14:12:07

问题


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


回答1:


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




回答2:


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.




回答3:


The best way to access the current_user in the Model(s) when using Authlogic is using Sentiant User - https://github.com/bokmann/sentient_user There is a full documentation on the there how to use it.



来源:https://stackoverflow.com/questions/2476624/authlogics-current-user-object-in-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!