Skip to content

Instantly share code, notes, and snippets.

@marka2g
Last active January 10, 2019 01:32
Show Gist options
  • Select an option

  • Save marka2g/8ca99d583b5d419f8f8e7737bf1760f5 to your computer and use it in GitHub Desktop.

Select an option

Save marka2g/8ca99d583b5d419f8f8e7737bf1760f5 to your computer and use it in GitHub Desktop.
Example of using skip_callbacks
# require 'active_record'
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db.sqlite3')
# ActiveRecord::Schema.define do
# create_table :users, force: true do |t|
# end
# end
class ActiveRecord::Base
cattr_accessor :skip_callbacks
end
class User < ActiveRecord::Base
after_create :send_greeting_email, unless: :skip_callbacks
private
def send_greeting_email
puts 'Sending greeting email..'
end
end
ActiveRecord::Base.skip_callbacks = true
User.create
# => Sending greeting email..
ActiveRecord::Base.skip_callbacks = false
User.create
# Will output nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment