2

There is a helper method in the application_controller:

  def current_user
      @current_user ||= User.find(session[:user_id])
      return @current_user
  end

I want to set up before_save callback in a model - so that the current_user.id will be saved when the activerecord is updated.

Basically, whenever the model is changed or modified and saved, I want to ensure the updated_by column is populated for that model/

But I'm not sure how to access the current user in the model?

Can anyone advise?

user3437721
  • 2,227
  • 4
  • 31
  • 61
  • what do you mean by helper method in the application_controller? – Bibek Sharma Aug 14 '15 at 15:30
  • at the top of the application_controller there is this line: helper_method :current_user – user3437721 Aug 14 '15 at 15:31
  • My sense is that a little more code will be helpful. Specifically, the Controller's action that is being called. And the model and it's `before_save`. – craig.kaminsky Aug 14 '15 at 15:32
  • I have not generated the before_save yet. I just want to be able to determine the current user from any model. The method I have posted seems to do that – user3437721 Aug 14 '15 at 15:33
  • You can't (well, shouldn't) access a helper method in a model ... but, if we can see the model (what attributes it has, etc.) and the controller's action, SO users can probably help you find the best way to get what you need accomplished in a "Railsy" fashion! – craig.kaminsky Aug 14 '15 at 15:35
  • Accessing the current user in the model breaks MVC decomposition. Depending on what you're trying to do, you either need to move some logic from model to controller or you need to pass certain data from `current_user` to the model. – eirikir Aug 14 '15 at 16:04

4 Answers4

0

It is not possible, current_user is available only in the controller. You should pass the current_user id in a method, for example :

In the controller method :

def action
  method_with_user_id(current_user.id)
end

in the model class definition :

def method_with_user_id(user_id)
  #things with the user id
end
Nicolas Maloeuvre
  • 3,069
  • 24
  • 42
  • Thanks I am doing this currently, but I wanted to create a before_save callback so that the updated user will all get updated for that model and I don't have to call methods everywhere – user3437721 Aug 14 '15 at 15:36
  • What means "so that the updated user will all get updated for that model" ? – Nicolas Maloeuvre Aug 14 '15 at 16:05
0

You can do something like this to access the instance variable declared in controller from model

class User < ActiveRecord::Base
  cattr_accessor :current_user
end 

 class ApplicationController < ActionController::Base
      before_filter :current_user

      def current_user
        @current_user ||= User.find(session[:user_id])
        User.current_user = @current_user
    end
  end
end
Bibek Sharma
  • 3,210
  • 6
  • 42
  • 64
0

Helpers methods are not available for models its just for the views.

I think that instead of set up before_save in the model and pass the current_user.id i think there is a relationship between your model and the user, so you should make the relationship, and pass the user object in the create action of that model.

And in your application controller you should have something like this.

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  def current_user
      @current_user ||= User.find(session[:user_id])
      return @current_user
  end
end

And in your model controller you have access to that method.

def create
  current_user.my_model.create(params_model)
end
Nivla
  • 35
  • 6
0

As a design choice, its not recommended to call controller helpers from your models. The model should not know about its controllers. That's not Railsy way of doing things.

To get a workaround you can set the instance variable @current_user in a class attribute of your controller:

class ApplicationController < ActionController::Base
  before_filter :set_current_user
  def set_current_user
    @current_user ||= User.find(session[:user_id])
    Account.current_user = @current_user
  end
end

Then, in your Model:

class User < ActiveRecord::Base
  cattr_accessor :current_user
end

Then just access the @current_user with Account.current_user in your Model.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110