title: Create if record does not exist
body: <p>I have 3 models in my rails app</p>
  
  <pre><code>class Contact &lt; ActiveRecord::Base
    belongs_to :survey, counter_cache: :contact_count
    belongs_to :voter
    has_many :contact_attempts
  end
  
  class Survey &lt; ActiveRecord::Base
    has_many :questions
    has_many :contacts
  end
  
  class Voter &lt; ActiveRecord::Base
    has_many :contacts
  end
  </code></pre>
  
  <p>the Contact consists of the voter_id and a survey_id. The Logic of my app is that a there can only be one  contact for a voter in any given survey.</p>
  
  <p>right now I am using the following code to enforce this logic. I query the contacts table for records matching the given voter_id and survey_id. if does not exist then it is created. otherwise it does nothing.</p>
  
  <pre><code>if !Contact.exists?(:survey_id =&gt; survey, :voter_id =&gt; voter)
     c = Contact.new
     c.survey_id = survey
     c.voter_id = voter
     c.save
  end
  </code></pre>
  
  <p>Obviously this requires a select and a insert query to create 1 potential contact. When I am adding potentially thousands of contacts at once.</p>
  
  <p>Right now I'm using Resque to allow this run in the background and away from the ui thread. What can I do to speed this up, and make it  more efficient?</p>