Recently while working on one of our project, there was a requirement of scheduling background job for processing some stuff once record is saved. So I written enqueue logic for job in after_save callback of record. But then job started giving unexpected results.
After long time of debugging I found that job was not able to fetch record from db which was just saved and after some researching on it I got to know that sometimes job scheduled in
fails to access that record as it tries to process it before transaction actually completes, so if you query database from job to fetch that record you will get
record not found exception.
solution for such scenarios is to use
after_commit callback which makes sure that your record is persisted in database and then your background job can access that record.
For more details on this issue of processing background job in after_save callback you can check following articles -