Authlogic's current_user object in models

前端 未结 3 874
星月不相逢
星月不相逢 2021-02-15 17:32

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          


        
相关标签:
3条回答
  • 2021-02-15 17:46

    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.

    0 讨论(0)
  • 2021-02-15 17:47

    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

    0 讨论(0)
  • 2021-02-15 17:57

    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.

    0 讨论(0)
提交回复
热议问题