Skip to content

Instantly share code, notes, and snippets.

@jtsaito
Created April 16, 2015 10:49
Show Gist options
  • Save jtsaito/29b34ecd19fef276ca73 to your computer and use it in GitHub Desktop.
Save jtsaito/29b34ecd19fef276ca73 to your computer and use it in GitHub Desktop.
Fix for race conditions in controllers

The following invalidates the ActiveRecord::QueryCache.

In an initializer for patching ActiveRecord:

ActiveRecord::Base.class_eval do
 
  def self.race_condition_safe(&block)
    begin
      yield
    rescue ActiveRecord::RecordNotUnique
      uncached { yield }
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment