Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 3, 2018 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/db492299dc49c7cd5f1959ee8b566d7a to your computer and use it in GitHub Desktop.
Save JoshCheek/db492299dc49c7cd5f1959ee8b566d7a to your computer and use it in GitHub Desktop.
Ordering AR callbacks (the real code uses ActiveResource)
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