Reputation:
I have a model with 5 after_create
callbacks. Do these callbacks get executed top to bottom? If not, what is the order of execution?
I know the docs list a precedence for each callback method, but they don't say anything about the order in which multiple callbacks of the same type are executed.
Upvotes: 3
Views: 1268
Reputation: 2677
Yes, callbacks of the same type are being executed from top to bottom - you can easily check it by yourself without digging into ActiveRecord code or searching for any documentation, for example this way:
class City < ActiveRecord::Base
after_create -> { log(1) }
after_create -> { log(2) }
after_create -> { log(3) }
private
def log(s)
File.open("/tmp/logger.txt", "a") { |f| f.puts s }
end
end
Now if you create a City
and inspect the file, output will always be
1
2
3
Upvotes: 3