Given this example:
class Server < ActiveRecord::Base
has_many :clients,:dependent => :destroy
after_destroy: delete_server_directory
end
class Client < ActiveRecord::Base
belongs_to :server
before_destroy :copy_some_important_stuff_from_the_server_directory_before_its_too_late
end
Will this be the order of destruction when I call server.destroy?
Server#clients, along withClient‘sbefore/after_destroycallbacksServerwill be destroyed- followed by the
Server‘safter_destroycallback
You can very easily test. I took your code, and implemented the callbacks with a simple call to
puts. Then launchedscript/consoleand had ActiveRecord log to the console:Set up some basic environment:
And here’s the gist of it:
So it looks like you were dead on target. 🙂
P.S.
after_destroy.before_destroy, as seen in your example.