Created
July 3, 2018 16:42
-
-
Save JoshCheek/db492299dc49c7cd5f1959ee8b566d7a to your computer and use it in GitHub Desktop.
Ordering AR callbacks (the real code uses ActiveResource)
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
require 'active_record' | |
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:' | |
ActiveRecord::Schema.define do | |
self.verbose = false | |
create_table(:users) { |t| t.string :name } | |
end | |
class User < ActiveRecord::Base | |
after_save :append_context | |
def append_context | |
self.name = name + ':parent' | |
end | |
end | |
class UserChild < User | |
def append_context | |
self.name += ':BEFORE' | |
super | |
self.name += ':AFTER' | |
end | |
end | |
UserChild.create!(name: 'original').name # => "original:BEFORE:parent:AFTER" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment