File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
activerecord/lib/active_record Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -373,6 +373,12 @@ class ReadOnlyRecord < ActiveRecordError
373
373
end
374
374
375
375
# 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].
376
382
class StrictLoadingViolationError < ActiveRecordError
377
383
end
378
384
Original file line number Diff line number Diff line change @@ -1763,7 +1763,15 @@ user.address.city # raises an ActiveRecord::StrictLoadingViolationError
1763
1763
user.comments.to_a # raises an ActiveRecord::StrictLoadingViolationError
1764
1764
```
1765
1765
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
+
1766
1772
[ `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
1767
1775
1768
1776
### ` strict_loading! `
1769
1777
@@ -1789,6 +1797,16 @@ user.comments.first.likes.to_a # raises an ActiveRecord::StrictLoadingViolationE
1789
1797
1790
1798
[ `strict_loading!` ] : https://api.rubyonrails.org/classes/ActiveRecord/Core.html#method-i-strict_loading-21
1791
1799
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
+
1792
1810
Scopes
1793
1811
------
1794
1812
You can’t perform that action at this time.
0 commit comments