Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active March 24, 2021 01:24
Show Gist options
  • Save stevepolitodesign/ed11b0bc29b40ad17090a78dbefbb03b to your computer and use it in GitHub Desktop.
Save stevepolitodesign/ed11b0bc29b40ad17090a78dbefbb03b to your computer and use it in GitHub Desktop.
Prevent Active Record Callbacks from creating new records
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
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