I finally solved this by using a class method instead of an instance method for my delay calls. Let me show you by way of example. Here's how I previously structured my delay calls:
def background_send_push_notifications
self.delay.send_push_notifications
end
def send_push_notifications
message = "#{self.user.name} liked your workout"
...
end
The issue I kept hitting was that it was common for a user to immediately unlike, right after liking something. This meant that the Like object was no more when the delayed_job tried to execute, and I'd get lots of "RecordNotFound" errors.
Now I have transitioned the delay call to a class method that does the object lookup in the background and returns if it no longer exists. Here's the new structure
def background_send_push_notifications
Like.delay.send_push_notifications(self.id)
end
def self.send_push_notifications(id)
like = Like.find_by_id(id)
like.send_push_notifications unless like.nil?
end
def send_push_notifications
message = "#{self.user.name} liked your workout"
...
end
Hope this helps somebody!