Last active
January 10, 2019 01:32
-
-
Save marka2g/8ca99d583b5d419f8f8e7737bf1760f5 to your computer and use it in GitHub Desktop.
Example of using skip_callbacks
This file contains hidden or 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
| # 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