Create a gist now

Instantly share code, notes, and snippets.

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