Skip to content

Commit 9483a19

Browse files
authored
Merge pull request rails#49850 from ansonhoyt/document-active_record-strict_loading_violation_error
Improve `strict_loading` documentation [ci skip]
2 parents 1b195b3 + 5101170 commit 9483a19

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

activerecord/lib/active_record/errors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ class ReadOnlyRecord < ActiveRecordError
373373
end
374374

375375
# Raised on attempt to lazily load records that are marked as strict loading.
376+
#
377+
# You can resolve this error by eager loading marked records before accessing
378+
# them. The
379+
# {Eager Loading Associations}[https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations]
380+
# guide covers solutions, such as using
381+
# {ActiveRecord::Base.includes}[rdoc-ref:QueryMethods#includes].
376382
class StrictLoadingViolationError < ActiveRecordError
377383
end
378384

guides/source/active_record_querying.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,15 @@ user.address.city # raises an ActiveRecord::StrictLoadingViolationError
17631763
user.comments.to_a # raises an ActiveRecord::StrictLoadingViolationError
17641764
```
17651765

1766+
To enable for all relations, change the
1767+
[`config.active_record.strict_loading_by_default`][] flag to `true`.
1768+
1769+
To send violations to the logger instead, change
1770+
[`config.active_record.action_on_strict_loading_violation`][] to `:log`.
1771+
17661772
[`strict_loading`]: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-strict_loading
1773+
[`config.active_record.strict_loading_by_default`]: configuring.html#config-active-record-strict-loading-by-default
1774+
[`config.active_record.action_on_strict_loading_violation`]: configuring.html#config-active-record-action-on-strict-loading-violation
17671775

17681776
### `strict_loading!`
17691777

@@ -1789,6 +1797,16 @@ user.comments.first.likes.to_a # raises an ActiveRecord::StrictLoadingViolationE
17891797

17901798
[`strict_loading!`]: https://api.rubyonrails.org/classes/ActiveRecord/Core.html#method-i-strict_loading-21
17911799

1800+
### `strict_loading` option on an association
1801+
1802+
We can also enable strict loading for a single association by providing the `strict_loading` option:
1803+
1804+
```ruby
1805+
class Author < ApplicationRecord
1806+
has_many :books, strict_loading: true
1807+
end
1808+
```
1809+
17921810
Scopes
17931811
------
17941812

0 commit comments

Comments
 (0)