title: Ruby on Rails: Find a record by an attribute not an id
body: <p>I'm very new to rails so please be patient with me.</p>
  
  <p>In short I'm trying to create a form in which guests to a wedding can enter a simple code (<code>invite_code</code>) and then RSVP. The from should take the <code>invite_code</code> and then take the use straight to the correct <code>invites#show</code> view.</p>
  
  <p>So far so good, but I'm stuck trying to get rails to find a record by something other than and <code>id</code>, I want to find by <code>invite_code</code>. Say I've got an <code>Invite</code> with an <code>id</code> of 4 and an <code>invite_id</code> of 1234, the form is finding the correct record when I enter '4' into the from but not '1234'. Here's some code to explain:</p>
  
  <p><strong>routes.rb</strong></p>
  
  <pre><code>get 'invites/search', to: 'invites#show', controller: :invites
  </code></pre>
  
  <p><strong>form</strong></p>
  
  <pre><code>...
  &lt;%= form_tag invites_search_path, method: :get do %&gt;
    &lt;%= label_tag :invite_code, "#" %&gt;
    &lt;%= text_field_tag :invite_code, nil %&gt;
    &lt;%= submit_tag "Search", name: nil %&gt;
  &lt;% end %&gt;
  ...
  </code></pre>
  
  <p><strong>invites_controller</strong></p>
  
  <pre><code>...
    def show
      if params.has_key?(:invite_code)
        @invite = Invite.find(params[:invite_code])
      else
        @invite = Invite.find(params[:id])
      end
    end
  ...
  </code></pre>
  
  <p><strong>rake routes output</strong></p>
  
  <pre><code>       Prefix Verb   URI Pattern                                   Controller#Action
     info_index GET    /info/index(.:format)                         info#index
        invites GET    /invites(.:format)                            invites#index
                POST   /invites(.:format)                            invites#create
     new_invite GET    /invites/new(.:format)                        invites#new
    edit_invite GET    /invites/:id/edit(.:format)                   invites#edit
         invite GET    /invites/:id(.:format)                        invites#show
                PATCH  /invites/:id(.:format)                        invites#update
                PUT    /invites/:id(.:format)                        invites#update
                DELETE /invites/:id(.:format)                        invites#destroy
  </code></pre>
  
  <p>invites_search GET    /invites/search(.:format)                     invites#show
               root GET    /                                             info#index</p>
  
  <p><strong>URL example</strong></p>
  
  <pre><code>.../invites/search?utf8=%E2%9C%93&amp;invite_code=1234
  
  "utf8"=&gt;"✓", "invite_code"=&gt;"1234", "id"=&gt;"search"
  </code></pre>
  
  <p>The application seems to be ignoring the <code>invite_id</code> part of if statement in the controller...</p>
  
  <p>Any help appreciated, it's taken me a long time to get this far...</p>