Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After Restore callback executing before commit #457

Closed
letiesperon opened this issue Oct 28, 2018 · 3 comments · Fixed by #559
Closed

After Restore callback executing before commit #457

letiesperon opened this issue Oct 28, 2018 · 3 comments · Fixed by #559
Labels

Comments

@letiesperon
Copy link

Hi!
I'm seeing that the after restore callback is getting run before the update commit.

gem 'paranoia', '~> 2.4'

class Flag < ApplicationRecord
  acts_as_paranoid

  after_restore :my_callback

  def my_callback
    p 'hi'
  end
end 

[7] pry(main)> flag.restore!
   (0.3ms)  BEGIN
  SQL (0.5ms)  UPDATE "flags" SET "deleted_at" = NULL, "updated_at" = '2018-10-28 12:55:34.948615' WHERE "flags"."id" = $1  [["id", 15]]
'hi'
   (5.9ms)  COMMIT

In my real callback I'm enqueuing a job that when executes it looks for the record in the db again, and if the job starts fast enough the state of the record is not yet updated.
Is this the expected behaviour for this callback? How can I get an after restore commit callback?

Thank you!

@fedefa
Copy link

fedefa commented May 2, 2019

+1

@fedefa
Copy link

fedefa commented May 2, 2019

@letiesperon As a workaround, you can do an after_commit with an update this way:

after_commit :after_restore, on: :update, if: :restored?

def restored?
  previous_changes[:deleted_at].present? && deleted_at.nil?
end

@letiesperon
Copy link
Author

@fedefa 🙌 👏 👏
I'll leave the issue open though since I don't think this is the expected behaviour, WDYT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants