views:

404

answers:

2

Hi all!

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

+2  A: 

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.

Damien MATHIEU
+3  A: 

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

Sniper
Thanks a lot - worked perfectly.
jriff
Wouldn't the current_user be just self? If you call current_user.save somewhere in the controller, then the :after_save :something will know everything about the user... Unless another user can respond as the current_user, you don't really need the "current_user" method I guess...
Hock
I was just trying to figure this out myself. Thanks!
BBonifield