backport runner fixes to 3-2-stable

Add a runner hook to Rails::Application and Rails::Engine that requires
ActiveRecord::Base to avoid circular constant loading when using observers.

This commit backports cc7dd66, c0ba0f0 and 8d01c61.
Merge pull request #7695 from benolee/backport_cc7dd66_and_c0ba0f0

backport runner fixes to 3-2-stable

Conflicts:
	railties/CHANGELOG.md