calls session[:user_id] twice

oleg.sobchuk.7 Posted by oleg.sobchuk.7 on September 08, 2013

Hello! When I check my app with Reek (gem), it reports me some errors, like calls session[:user_id] twice in

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

i have only one idea, is

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

so, have you more ideas?

Answers

hck Answered by hck on September 08, 2013

You can actually replace your finder with where().first:

def current_user
  @current_user ||= User.where(id: session[:user_id]).first
end

This will return user by :user_id key stored in session, or return nil if no such key in session or no such user_id in database.