Last active
March 24, 2021 01:24
-
-
Save stevepolitodesign/ed11b0bc29b40ad17090a78dbefbb03b to your computer and use it in GitHub Desktop.
Prevent Active Record Callbacks from creating new records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :create_message do | |
desc "Creates a Message but prevents Notifications from being created on the after_create Callback" | |
task :perform, [:message_content] => :environment do |task, args| | |
# https://api.rubyonrails.org/classes/ActiveRecord/Suppressor.html | |
Notification.suppress do | |
Message.create(content: args.message_content) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message < ApplicationRecord | |
after_create :create_notification | |
private | |
def create_notification | |
Notification.create(content: "Foo bar baz") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment