File tree Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Original file line number Diff line number Diff line change
1
+ * Allow to configure ` strict_loading_mode ` globally or within a model.
2
+
3
+ Defaults to ` :all ` , can be changed to ` :n_plus_one_only ` .
4
+
5
+ * Garen Torikian*
6
+
1
7
* Add ` ActiveRecord::Relation#readonly? ` .
2
8
3
9
Reflects if the relation has been marked as readonly.
Original file line number Diff line number Diff line change @@ -89,6 +89,7 @@ def self.configurations
89
89
class_attribute :belongs_to_required_by_default , instance_accessor : false
90
90
91
91
class_attribute :strict_loading_by_default , instance_accessor : false , default : false
92
+ class_attribute :strict_loading_mode , instance_accessor : false , default : :all
92
93
93
94
class_attribute :has_many_inversing , instance_accessor : false , default : false
94
95
@@ -784,7 +785,7 @@ def init_internals
784
785
785
786
@primary_key = klass . primary_key
786
787
@strict_loading = klass . strict_loading_by_default
787
- @strict_loading_mode = :all
788
+ @strict_loading_mode = klass . strict_loading_mode
788
789
789
790
klass . define_attribute_methods
790
791
end
Original file line number Diff line number Diff line change @@ -98,6 +98,20 @@ def test_strict_loading_n_plus_one_only_mode_does_not_eager_load_child_associati
98
98
end
99
99
end
100
100
101
+ def test_default_mode_is_all
102
+ developer = Developer . first
103
+ assert_predicate developer , :strict_loading_all?
104
+ end
105
+
106
+ def test_default_mode_can_be_changed_globally
107
+ developer = Class . new ( ActiveRecord ::Base ) do
108
+ self . strict_loading_mode = :n_plus_one_only
109
+ self . table_name = "developers"
110
+ end . new
111
+
112
+ assert_predicate developer , :strict_loading_n_plus_one_only?
113
+ end
114
+
101
115
def test_strict_loading
102
116
Developer . all . each { |d | assert_not d . strict_loading? }
103
117
Developer . strict_loading . each { |d | assert_predicate d , :strict_loading? }
Original file line number Diff line number Diff line change @@ -1284,6 +1284,12 @@ changed to `:log` to send violations to the logger instead of raising.
1284
1284
Is a boolean value that either enables or disables strict_loading mode by
1285
1285
default. Defaults to `false`.
1286
1286
1287
+ #### `config.active_record.strict_loading_mode`
1288
+
1289
+ Sets the mode in which strict loading is reported. Defaults to `:all`. It can be
1290
+ changed to `:n_plus_one_only` to only report when loading associations that will
1291
+ lead to an N + 1 query.
1292
+
1287
1293
#### `config.active_record.warn_on_records_fetched_greater_than`
1288
1294
1289
1295
Allows setting a warning threshold for query result size. If the number of
You can’t perform that action at this time.
0 commit comments