Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access devise current_user from model

Just start to develop with devise for my app authentication, however running into trouble with accessing current_user from a model. I have no problem accessing this from a controller.

For my scenario I need to get the current user id to keep track who has edit the blog post, which I save the user id with before_save filter.

Appreciate your help!

Thanks

like image 880
TonyTakeshi Avatar asked Nov 26 '22 19:11

TonyTakeshi


1 Answers

You can only pass it down in a parameter.

class SomeModel 
  def my_nifty_method(user)
  end
end

in controller:

class SomeModelsController < ApplicationController
  def some_method
    sm = SomeModel.new
    sm.my_nifty_method(current_user)
  end
end
like image 185
m4risU Avatar answered Dec 23 '22 07:12

m4risU