title: Simple search in Mongoid using mongoid_search gem
body: <p>I'm trying to implement search in one of my models. I'm using the gem "mongoid_search" and I'm mixing it with the railscast #240 on search. It seems to be returning an empty array and I can't figure out why. Can anyone point me in the right direction?</p>
  
  <p>My model looks like this:</p>
  
  <pre><code>include Mongoid::Search 
  
  attr_accessible :twitterUsername, :twitterName, :twitterLocation, :twitterDescription, :projects
  
  def self.search(search)
      if search
          search_in :twitterUsername, :twitterName, :twitterLocation, :twitterDescription, :projects
      else
          scoped
      end
  end
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>def index
      @users = User.search(params[:search])
  
      respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @users }
      end
  end
  </code></pre>
  
  <p>And my view:</p>
  
  <pre><code>&lt;%= form_tag users_path, :method =&gt; 'get', :id =&gt; "users_search" do %&gt;
      &lt;p&gt;
          &lt;%= text_field_tag :search, params[:search] %&gt;
          &lt;%= submit_tag "Search", :name =&gt; nil %&gt;
      &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>I define the twitterUsername (sorry for the bad naming convention) in the model as well:</p>
  
  <pre><code>field :provider, :type =&gt; String
  field :uid, :type =&gt; String
  field :twitterName, :type =&gt; String
  field :twitterUsername, :type =&gt; String
  field :twitterAvatar, :type =&gt; String
  field :twitterUrl, :type =&gt; String
  field :twitterPortfolioUrl, :type =&gt; String
  field :twitterLocation, :type =&gt; String
  field :twitterDescription, :type =&gt; String
  field :email, :type =&gt; String
  field :description, :type =&gt; String
  field :portfolioUrl, :type =&gt; String
  field :watchers, :type =&gt; Integer
  field :inspirations, :type =&gt; Integer
  field :invitees, :type =&gt; Integer
  field :shot, :type =&gt; String
  field :remote_image_url, :type =&gt; String
  field :hireMe, :type =&gt; Boolean
  field :projects, :type =&gt; Integer
  key :twitterUsername
  
  
  def self.create_with_omniauth(auth)
      create! do |user|
          user.provider = auth['provider']
          user.uid = auth['uid']
          if auth['info']
              user._id = auth['info']['nickname'] || ""
              user.twitterName = auth['info']['name'] || ""
              user.twitterUsername = auth['info']['nickname'] || ""
              user.twitterAvatar = auth['info']['image'] || ""
              user.twitterUrl = auth['info']['urls']['Twitter'] || ""
              user.twitterPortfolioUrl = auth['info']['urls']['Website'] || ""
              user.twitterLocation = auth['info']['location'] || ""
              user.twitterDescription = auth['info']['description'] || ""
          end
      end
  end
  </code></pre>
  
  <p>UPDATE 2</p>
  
  <p>If I count the users, it seems to be returning a single user (which is correct since I'm the only one signed up right now):</p>
  
  <pre><code>&lt;% if @users.count &gt; 0 %&gt;
    &lt;%= @users.count %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>if I do:</p>
  
  <pre><code>&lt;% if @users.count &gt; 0 %&gt;
    &lt;%= @users.twitterUsername %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>it gives me this error: undefined method `twitterUsername' for User:Class.</p>
  
  <p>UPDATE 3</p>
  
  <p>It gives me an error when I try to run tail log/development.log:</p>
  
  <pre><code>± % tail log/development.log                                                                                                             
  22:     
  app/views/users/index.html.erb:19:in `block in _app_views_users_index_html_erb__1766206529136141992_2490506940'
  app/views/users/index.html.erb:18:in `each'
  app/views/users/index.html.erb:18:in `_app_views_users_index_html_erb__1766206529136141992_2490506940'
  app/controllers/users_controller.rb:7:in `index'
  
  
  Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.3ms)
  Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
  Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (13.9ms)
  </code></pre>
  
  <p>UPDATE 4</p>
  
  <p>I get this in my console if when I run a search:</p>
  
  <pre><code>Started GET "/users?utf8=%E2%9C%93&amp;search=holgersindbaek" for 127.0.0.1 at 2012-04-10     23:24:55 +0200
  Processing by UsersController#index as HTML
  Parameters: {"utf8"=&gt;"✓", "search"=&gt;"holgersindbaek"}
  MONGODB (1ms) flow04_development['users'].find({:_id=&gt;"holgersindbaek"}).limit(-1).sort([[:_id, :asc]])
  Rendered users/index.html.erb within layouts/application (4.7ms)
  Completed 500 Internal Server Error in 8ms
  
  ActionView::Template::Error (undefined method `id' for User:Class):
  22:         &lt;% if current_user == user %&gt;
  23: 
  24:         &lt;% else %&gt;
  25:           &lt;% if current_user.follower_of?(user) %&gt;
  26:             &lt;%= link_to "Unfollow", unfollow_user_path(user), :remote =&gt; true,     :class=&gt;'unfollow' %&gt;
  27:           &lt;% else %&gt;
  28:             &lt;%= link_to "Follow", follow_user_path(user), :remote =&gt; true, :class=&gt;'follow' %&gt;
  app/views/users/index.html.erb:25:in `block in _app_views_users_index_html_erb__83132873031271873_2505118720'
  app/views/users/index.html.erb:18:in `each'
  app/views/users/index.html.erb:18:in `_app_views_users_index_html_erb__83132873031271873_2505118720'
  app/controllers/users_controller.rb:7:in `index'
  </code></pre>