You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-5Lines changed: 11 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,14 @@
1
1
# A::NtiMannerKickCourse
2
2
3
+
## Introduction
3
4
Rails speeds up the startup process by lazily loading classes for components like ActiveRecord::Base and ActionController::Base. Additionally, the behavior of lazy loading is essential for applying configuration settings to these components. Without lazy loading, some configurations might not be applied as expected.
4
5
6
+
### Why Lazy Loading Matters
7
+
Without lazy loading, certain settings might not be applied in time.
8
+
5
9
For example, consider the following scenario:
6
10
11
+
### Example: Upgrading from Rails 7.0 to 7.1
7
12
When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates a file named `config/initializers/new_framework_defaults_7_1.rb`. This file helps incrementally enable new default settings for Rails 7.1. Initially, all settings in this file are commented out. Uncommenting the following setting will enable it:
8
13
9
14
```ruby
@@ -12,8 +17,9 @@ When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates
As the comment suggests, this setting ensures that ActionController::Parameters instances are no longer treated as equivalent to Hash. However, if ActionController::Base is eager-loaded, this configuration will not behave as expected. Why does this happen?
18
24
19
25
Configuration settings like `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality` are not automatically effective. These settings are applied to components like ActionController only during the Rails initialization process.
@@ -32,7 +38,7 @@ This assignment is wrapped in an `ActiveSupport.on_load(:action_controller, run_
32
38
ActiveSupport.on_load(:action_controller, run_once:true) do
33
39
# Configuration assignment logic
34
40
end
35
-
````
41
+
```
36
42
37
43
Now, consider a gem "add_some_function_to_controller" with the following code:
38
44
@@ -52,8 +58,7 @@ Since all gems listed in the Gemfile are required in config/application.rb, the
52
58
53
59
As a result, the configuration logic for ActionController is executed before the initializer file `config/initializers/new_framework_defaults_7_1.rb`. This means that the setting `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false` is not applied in time.
54
60
55
-
Fixing the Issue
56
-
61
+
### Fixing the Issue
57
62
To solve this, modify the gem code as follows:
58
63
59
64
```ruby
@@ -76,6 +81,7 @@ This resolves the issue, as the setting in config/initializers/new_framework_def
76
81
77
82
While this specific case is likely a common issue, other problems may also arise from changes to the initialization order. To ensure reliable behavior, it is critical to keep all component loading deferred during Rails initialization. However, manually checking for potential issues with lazy loading is impractical.
78
83
84
+
### Using a-nti_manner_kick_course
79
85
Using a-nti_manner_kick_course can help detect libraries or application code (like add_some_function_to_controller) that interfere with lazy loading, allowing you to maintain proper initialization behavior.
If any code fails to lazy loading, the process will exit with status code 1. This command suggests which lines are eager loading Rails code. It is a good idea to regularly check in CI if lazy loading is working correctly.
112
118
113
-
You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
119
+
You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
114
120
115
121
For more details about ActiveSupport.on_load, check [the official Rails documentation](https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html).
0 commit comments