Skip to content

Instantly share code, notes, and snippets.

@gilbertwat
Created March 27, 2018 09:01
Show Gist options
  • Save gilbertwat/3295c5e4e459079b9fa09052ab767f58 to your computer and use it in GitHub Desktop.
Save gilbertwat/3295c5e4e459079b9fa09052ab767f58 to your computer and use it in GitHub Desktop.
Avoid Deadlock
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