title: In Rails, what is the best way to update a record or create a new one if it doesn't exist?
body: <p>I have a create statement for some models, but it’s creating a record within a join table regardless of whether the record already exists.</p>
  
  <p>Here is what my code looks like:</p>
  
  <pre><code>@user = User.find(current_user)
  @event = Event.find(params[:id])
  for interest in @event.interests
   @user.choices.create(:interest =&gt; interest, :score =&gt; 4)
  end
  </code></pre>
  
  <p>The problem is that it creates records no matter what. I would like it to create a record only if no record already exists; if a record does exist, I would like it to take the attribute of the found record and add or subtract 1.</p>
  
  <p>I’ve been looking around have seen something called <code>find_or_create_by</code>. What does this do when it finds a record? I would like it to take the current <code>:score</code> attribute and add 1.</p>
  
  <p>Is it possible to find or create by <code>id</code>? I’m not sure what attribute I would find by, since the model I’m looking at is a join model which only has <code>id</code> foreign keys and the score attribute.</p>
  
  <p>I tried </p>
  
  <pre><code>@user.choices.find_or_create_by_user(:user =&gt; @user.id, :interest =&gt; interest, :score =&gt; 4)
  </code></pre>
  
  <p>but got </p>
  
  <blockquote>
    <p>undefined method <code>find_by_user</code></p>
  </blockquote>
  
  <p>What should I do?</p>