user577808
user577808

Reputation: 2527

Rails 3 helper method available in model

I'm upgrading an app to Rails 3. We have an Application Controller like the following:

class ApplicationController < ActionController::Base
     helper_method :current_user

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

Because we define "helper_method", "current_user" is available to all the views. It's available to the controllers because they all inherit from class ApplicationController.

However, when we were on 2.3.8, access to "current_user" was available through a model, but now it's not. Is there a way to get this exposed to the models?

Upvotes: 10

Views: 10632

Answers (1)

Kelly
Kelly

Reputation: 41601

I could suggest moving current_user into a helper like UserHelper, then you can include it in your model with include UserHelper.

You'll also have to include UserHelper in your ApplicationController, but helper :user will include that like normal into your views too.

Upvotes: 9

Related Questions