Rails Lazy Load Hooks Cheatsheet
ActiveSupport in Rails provides hooks for when classes are lazily loaded. You can use this to add features to your Rails application when things are loaded.
Rails provides Lazy Load Hooks through ActiveSupport. We can listen for when something loads and execute a block when it runs with on_load
.
Here's an example how to use it:
ActiveSupport.on_load(:active_record) do
include Multitenancy
end
Available Lazy Load Hooks in Rails
:action_cable
:action_cable_channel
:action_cable_channel_test_case
:action_cable_connection
:action_cable_connection_test_case
:action_cable_test_case
:action_controller
:action_controller_api
:action_controller_base
:action_controller_test_case
:action_dispatch_integration_test
:action_dispatch_request
:action_dispatch_response
:action_dispatch_system_test_case
:action_mailbox
:action_mailbox_inbound_email
:action_mailbox_record
:action_mailbox_test_case
:action_mailer
:action_mailer_test_case
:action_text_content
:action_text_encrypted_rich_text
:action_text_record
:action_text_rich_text
:action_view
:action_view_test_case
:active_job
:active_job_test_case
:active_model
:active_record
:active_record_fixture_set
:active_record_mysql2adapter
:active_record_postgresqladapter
:active_record_sqlite3adapter
:active_record_trilogyadapter
:active_storage_attachment
:active_storage_blob
:active_storage_record
:active_storage_variant_record
:active_support_test_case
:after_initialize
:after_routes_loaded
:before_configuration
:before_eager_load
:before_initialize
:i18n
:message_pack
For more details, check out the Rails Engines Guide - Available Load Hooks