How to access devus current_user from a model

Just start developing using the application to authenticate my application, however, faced with the problem of accessing current_user from the model. I have no problem accessing it from the controller.

For my scenario, I need to get the current user ID in order to keep track of who is editing the blog post, and I save the user ID with the before_save filter.

Appreciate your help!

thank

+5
source share
3 answers

You can only pass it in a parameter.

class SomeModel 
  def my_nifty_method(user)
  end
end

in the controller:

class SomeModelsController < ApplicationController
  def some_method
    sm = SomeModel.new
    sm.my_nifty_method(current_user)
  end
end
+3
source

. 'c', user_id .

before_create {|c| c.user_id = Authorization.current_user.id}

RoR, .

-1

Why are you trying to access this method in the model? You can access identifier attributes via self.id(within the model)

-5
source

All Articles