Skip to content

Commit 5ff9915

Browse files
committed
Allow one to set strict_loading_mode globally
1 parent 8336aa7 commit 5ff9915

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
* Add `ActiveRecord::Relation#readonly?`.
28

39
Reflects if the relation has been marked as readonly.

activerecord/lib/active_record/core.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def self.configurations
8989
class_attribute :belongs_to_required_by_default, instance_accessor: false
9090

9191
class_attribute :strict_loading_by_default, instance_accessor: false, default: false
92+
class_attribute :strict_loading_mode, instance_accessor: false, default: :all
9293

9394
class_attribute :has_many_inversing, instance_accessor: false, default: false
9495

@@ -784,7 +785,7 @@ def init_internals
784785

785786
@primary_key = klass.primary_key
786787
@strict_loading = klass.strict_loading_by_default
787-
@strict_loading_mode = :all
788+
@strict_loading_mode = klass.strict_loading_mode
788789

789790
klass.define_attribute_methods
790791
end

activerecord/test/cases/strict_loading_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ def test_strict_loading_n_plus_one_only_mode_does_not_eager_load_child_associati
9898
end
9999
end
100100

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+
101115
def test_strict_loading
102116
Developer.all.each { |d| assert_not d.strict_loading? }
103117
Developer.strict_loading.each { |d| assert_predicate d, :strict_loading? }

guides/source/configuring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,12 @@ changed to `:log` to send violations to the logger instead of raising.
12841284
Is a boolean value that either enables or disables strict_loading mode by
12851285
default. Defaults to `false`.
12861286
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+
12871293
#### `config.active_record.warn_on_records_fetched_greater_than`
12881294
12891295
Allows setting a warning threshold for query result size. If the number of

0 commit comments

Comments
 (0)