Trigger callback after_commit but NOT as a result of touch Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!after_find callback broken after upgrade to rails3How to avoid using callbacks in railsTrigger jquery based on model eventssunspot callback after_commit?Change name of updated_at column in ActiveRecord / RailsHow can a deadlock occur when no row or table locks are set? (caused by rails ActiveRecord#touch)Altering an associated model using a before_validation callback`after_commit` callback on `update` doesn't triggerin a Rails after_save callback, would the commit have finished and the change be accessible for a Sidekiq jobInline `after_commit` Callbacks in Rails
Is there hard evidence that the grant peer review system performs significantly better than random?
Amount of permutations on an NxNxN Rubik's Cube
Performance gap between vector<bool> and array
What does it mean that physics no longer uses mechanical models to describe phenomena?
How do I find out the mythology and history of my Fortress?
Do any jurisdictions seriously consider reclassifying social media websites as publishers?
How to react to hostile behavior from a senior developer?
Effects on objects due to a brief relocation of massive amounts of mass
Selecting user stories during sprint planning
Is grep documentation about ignoring case wrong, since it doesn't ignore case in filenames?
Is there any word for a place full of confusion?
AppleTVs create a chatty alternate WiFi network
Did Krishna say in Bhagavad Gita "I am in every living being"
An adverb for when you're not exaggerating
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
How come Sam didn't become Lord of Horn Hill?
Generate an RGB colour grid
Are all finite dimensional hilbert spaces isomorphic to spaces with Euclidean norms?
How could we fake a moon landing now?
What is the topology associated with the algebras for the ultrafilter monad?
Can anything be seen from the center of the Boötes void? How dark would it be?
Do I really need to have a message in a novel to appeal to readers?
Why wasn't DOSKEY integrated with COMMAND.COM?
SF book about people trapped in a series of worlds they imagine
Trigger callback after_commit but NOT as a result of touch
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!after_find callback broken after upgrade to rails3How to avoid using callbacks in railsTrigger jquery based on model eventssunspot callback after_commit?Change name of updated_at column in ActiveRecord / RailsHow can a deadlock occur when no row or table locks are set? (caused by rails ActiveRecord#touch)Altering an associated model using a before_validation callback`after_commit` callback on `update` doesn't triggerin a Rails after_save callback, would the commit have finished and the change be accessible for a Sidekiq jobInline `after_commit` Callbacks in Rails
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to trigger my callback whenever an object is committed. But I don't want the callback to trigger if the after_commit is a result of the object simply being touched. I've looked into after_update , after_update_commit , and after_save, but none of these seem to match the behavior i'm describing. Is there a way to do this other than to have my callback reload the object from the database and verify that a column other than updated_at has been modified before proceeding? I'm on Rails 5.2.
ruby-on-rails
add a comment |
I'm trying to trigger my callback whenever an object is committed. But I don't want the callback to trigger if the after_commit is a result of the object simply being touched. I've looked into after_update , after_update_commit , and after_save, but none of these seem to match the behavior i'm describing. Is there a way to do this other than to have my callback reload the object from the database and verify that a column other than updated_at has been modified before proceeding? I'm on Rails 5.2.
ruby-on-rails
after_saveseems like the way to go. It's not executed ontouch(after_commit is).
– Mark Merritt
Mar 8 at 20:18
but I need this callback to occur only after the transaction is complete, whereasafter_savewill get called even if the sql query fails
– stackPusher
Mar 8 at 20:42
When a call tosavefails, it returns false...which will stop the callback chain (after_savewill not be executed)
– Mark Merritt
Mar 9 at 1:20
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
here is a decent article highlighting another reason whyafter_savecould lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…
– stackPusher
Mar 13 at 17:33
add a comment |
I'm trying to trigger my callback whenever an object is committed. But I don't want the callback to trigger if the after_commit is a result of the object simply being touched. I've looked into after_update , after_update_commit , and after_save, but none of these seem to match the behavior i'm describing. Is there a way to do this other than to have my callback reload the object from the database and verify that a column other than updated_at has been modified before proceeding? I'm on Rails 5.2.
ruby-on-rails
I'm trying to trigger my callback whenever an object is committed. But I don't want the callback to trigger if the after_commit is a result of the object simply being touched. I've looked into after_update , after_update_commit , and after_save, but none of these seem to match the behavior i'm describing. Is there a way to do this other than to have my callback reload the object from the database and verify that a column other than updated_at has been modified before proceeding? I'm on Rails 5.2.
ruby-on-rails
ruby-on-rails
asked Mar 8 at 19:52
stackPusherstackPusher
2,3121826
2,3121826
after_saveseems like the way to go. It's not executed ontouch(after_commit is).
– Mark Merritt
Mar 8 at 20:18
but I need this callback to occur only after the transaction is complete, whereasafter_savewill get called even if the sql query fails
– stackPusher
Mar 8 at 20:42
When a call tosavefails, it returns false...which will stop the callback chain (after_savewill not be executed)
– Mark Merritt
Mar 9 at 1:20
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
here is a decent article highlighting another reason whyafter_savecould lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…
– stackPusher
Mar 13 at 17:33
add a comment |
after_saveseems like the way to go. It's not executed ontouch(after_commit is).
– Mark Merritt
Mar 8 at 20:18
but I need this callback to occur only after the transaction is complete, whereasafter_savewill get called even if the sql query fails
– stackPusher
Mar 8 at 20:42
When a call tosavefails, it returns false...which will stop the callback chain (after_savewill not be executed)
– Mark Merritt
Mar 9 at 1:20
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
here is a decent article highlighting another reason whyafter_savecould lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…
– stackPusher
Mar 13 at 17:33
after_save seems like the way to go. It's not executed on touch (after_commit is).– Mark Merritt
Mar 8 at 20:18
after_save seems like the way to go. It's not executed on touch (after_commit is).– Mark Merritt
Mar 8 at 20:18
but I need this callback to occur only after the transaction is complete, whereas
after_save will get called even if the sql query fails– stackPusher
Mar 8 at 20:42
but I need this callback to occur only after the transaction is complete, whereas
after_save will get called even if the sql query fails– stackPusher
Mar 8 at 20:42
When a call to
save fails, it returns false...which will stop the callback chain (after_save will not be executed)– Mark Merritt
Mar 9 at 1:20
When a call to
save fails, it returns false...which will stop the callback chain (after_save will not be executed)– Mark Merritt
Mar 9 at 1:20
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
here is a decent article highlighting another reason why
after_save could lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…– stackPusher
Mar 13 at 17:33
here is a decent article highlighting another reason why
after_save could lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…– stackPusher
Mar 13 at 17:33
add a comment |
1 Answer
1
active
oldest
votes
Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:
# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> transaction_changed_attributes.keys == ['updated_at']
this way the callback isnt executed if the after_commit is the result of just a touch.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070104%2ftrigger-callback-after-commit-but-not-as-a-result-of-touch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:
# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> transaction_changed_attributes.keys == ['updated_at']
this way the callback isnt executed if the after_commit is the result of just a touch.
add a comment |
Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:
# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> transaction_changed_attributes.keys == ['updated_at']
this way the callback isnt executed if the after_commit is the result of just a touch.
add a comment |
Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:
# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> transaction_changed_attributes.keys == ['updated_at']
this way the callback isnt executed if the after_commit is the result of just a touch.
Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:
# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> transaction_changed_attributes.keys == ['updated_at']
this way the callback isnt executed if the after_commit is the result of just a touch.
answered Mar 14 at 22:52
stackPusherstackPusher
2,3121826
2,3121826
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070104%2ftrigger-callback-after-commit-but-not-as-a-result-of-touch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
after_saveseems like the way to go. It's not executed ontouch(after_commit is).– Mark Merritt
Mar 8 at 20:18
but I need this callback to occur only after the transaction is complete, whereas
after_savewill get called even if the sql query fails– stackPusher
Mar 8 at 20:42
When a call to
savefails, it returns false...which will stop the callback chain (after_savewill not be executed)– Mark Merritt
Mar 9 at 1:20
the other issue is I don't want the transaction to be rolled back if the callback fails. the callback concerns itself with creating sidekiq jobs which will be requeued at a later time if there is a problem with sidekiq or redis or whatever, but shouldn't rollback the transaction. See github.com/mperham/sidekiq/issues/3906 for someone else explaining that particular concern probably more eloquently than i can
– stackPusher
Mar 13 at 17:12
here is a decent article highlighting another reason why
after_savecould lead to problems if used for background jobs: rubyinrails.com/2018/12/03/…– stackPusher
Mar 13 at 17:33