Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created April 25, 2019 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/8e502bde1f64827e73a33d1307b3ae05 to your computer and use it in GitHub Desktop.
Save krisleech/8e502bde1f64827e73a33d1307b3ae05 to your computer and use it in GitHub Desktop.
ActiveRecord locking
# Prevent other processes/threads from updating rows, uses "FOR UPDATE" lock by default:
Recruitment::CpmsTotal.transaction do
totals = Recruitment::CpmsTotal.not_sent.lock.each # [1]
sleep(40)
end
# [1] each forces query to happen, #all on its own does not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment