title: Why do two methods that are logically identical not perform the same task?
body: <p>In a Rails application, I have two methods in my application controller. One of them authenticates users (i.e. checks if a user is logged in, and redirects them to login if not). The other performs the first half of that task (i.e. checks if a user is logged in).</p>
  
  <p>When the check is complete, if a user is logged in, both of these methods supposedly set a <code>@current_user</code> variable to the user object for the user who is currently signed in, by using <code>User.find</code>.</p>
  
  <p>The former:</p>
  
  <pre><code>protected
  def authenticate_user
    if session[:user_id]
       # set current user object to @current_user object variable
      @current_user = User.find session[:user_id]
      return true
    else
      flash[:notice] = "You must log in first."
      flash[:color] = "invalid"
      redirect_to(:controller =&gt; 'sessions', :action =&gt; 'login')
      return false
    end
  end
  </code></pre>
  
  <p>The latter:</p>
  
  <pre><code>def check_login_status
    if session[:user_id]
      @current_user = User.find session[:user_id]
      return true
    end
  end
  </code></pre>
  
  <p>As you can see, the logic for the first half of each of these methods is identical. However, <code>authenticate_user</code> correctly sets the <code>@current_user</code> variable; <code>check_login_status</code> doesn't set it at all (as in, the check in the layout file says <code>@current_user.nil? == true</code>.</p>
  
  <p>This is the relevant part of the layout file:</p>
  
  <pre><code>&lt;% if not @current_user.nil? %&gt;
    Logged in as &lt;%= @current_user.username %&gt; —
    &lt;a href="/logout"&gt;log out&lt;/a&gt; —
    &lt;a href="/dashboard"&gt;dashboard&lt;/a&gt; —
    &lt;a href="/contacts"&gt;contacts&lt;/a&gt; —
    &lt;a href="/help"&gt;help&lt;/a&gt;
    &lt;% if @current_user.is_admin %&gt;
      — &lt;a href="/admin"&gt;admin&lt;/a&gt;
    &lt;% end %&gt;
  &lt;% else %&gt;
    &lt;a href="/login"&gt;log in&lt;/a&gt; —
    &lt;a href="/sign-up"&gt;sign up&lt;/a&gt; —
    &lt;a href="/help"&gt;help&lt;/a&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I get the second set of links shown, indicating that I'm logged out.</p>
  
  <p>So, why does the latter not set the <code>@current_user</code> variable correctly? Does it have anything to do with the <code>protected</code> marking (though somehow I doubt it)?</p>