title: current_user helper method returning error in mongoid backed authentication
body: <p>I am using omniauth-twitter gem for authentication. And my rails app is backed by Mongodb. </p>
  
  <p>When clicked on <strong>Login</strong> it opened the twitter profile. I entered the credentials and got redirected back to the app with an error saying </p>
  
  <pre><code>unknown operator: $oid (2)
  
      def current_user
        @current_user ||= User.find_by(id: session[:user_id])
      end
  
      helper_method :current_user
  </code></pre>
  
  <p>It is pointing error at the <code>@current_user</code> line. But when I checked the database for the user created by doing <code>User.last</code>, I can see a user object.</p>
  
  <p><strong>User.rb</strong> file</p>
  
  <pre><code>class User
    include Mongoid::Document
  
    field :provider, type: String
    field :uid, type: String
    field :name, type: String
    field :location, type: String
    field :image_url, type: String
    field :url, type: String
    index({ provider: 1, uid: 1 }, { unique: true})
  
    class &lt;&lt; self
        def from_omniauth(auth_hash)
          user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
          user.name = auth_hash['info']['name']
          user.location = auth_hash['info']['location']
          user.image_url = auth_hash['info']['image']
          user.url = auth_hash['info']['urls']['Twitter']
          user.save!
          user
        end
    end
  end
  </code></pre>
  
  <p>Then I created a sessions controller and looks as follows.</p>
  
  <pre><code>class SessionsController &lt; ApplicationController
  
    def create
      begin
      @user = User.from_omniauth(request.env['omniauth.auth'])
      session[:user_id] = @user.id
      flash[:success] = "Welcome, #{@user.name}!"
        rescue
          flash[:warning] = "There was an error while trying to authenticate you..."
      end
      redirect_to root_path
    end
  
    def destroy
      if current_user
       session.delete(:user_id)
       flash[:success] = 'See you!'
      end
     redirect_to root_path
    end
  end
  </code></pre>
  
  <p>Can someone please help me fix this ?</p>