title: ActiveRecord: ensure only one record has specific attribute value?
body: <p>I have a Races table with many races in various states. But I need to ensure that only one race is marked as <em>current = true</em>. Here is what I have been using in the Race model validation.</p>
  
  <pre><code># current: boolean
  validate :only_one_current
  
  private
  def only_one_current
    if self.current &amp;&amp; (Race.current_race.id != self.id)
      errors.add(:base, "Races can have only one current race")
    end
  end
  </code></pre>
  
  <p>This seems to work most of the time, but occasionally it does not and I'm not sure why. When it doesn't work it disallows the saving of a new record with <em>current = t</em> just after a different record that was current is deleted. I think it has to do with AR's persistence.</p>
  
  <p>There must be a better way to do this?</p>