Created
March 27, 2018 09:01
-
-
Save gilbertwat/3295c5e4e459079b9fa09052ab767f58 to your computer and use it in GitHub Desktop.
Avoid Deadlock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def must_deadlock | |
new_waypoints = get_new_waypoints | |
order.transaction do | |
order.waypoints.destroy_all | |
new_waypoints.each |waypoint| do | |
Order.waypoints.create!(waypoint) | |
end | |
end | |
end | |
def no_deadlock | |
new_waypoints = get_new_waypoints.pluck(:id) | |
old_waypoints = order.waypoints.all.pluck(:id) | |
order.transaction do | |
removed_ids = old_waypoints - new_waypoints | |
order.waypoints.delete(removed_ids) | |
added_ids = new_waypoints - old_waypoints | |
added_ids.each |id| do | |
order.waypoints.create(Waypoint.find(id)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment