Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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