Skip to content

Commit

Permalink
Merge pull request #49329 from p8/guides/strict-loading-with-mode
Browse files Browse the repository at this point in the history
Improve `strict_loading` documentation [ci-skip]
  • Loading branch information
p8 committed Sep 20, 2023
2 parents 0723e6c + 677fa58 commit 6f27e92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 7 additions & 4 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,9 @@ def strict_loading?
#
# user = User.first
# user.strict_loading! # => true
# user.comments
# user.address.city
# => ActiveRecord::StrictLoadingViolationError
# user.comments.to_a
# => ActiveRecord::StrictLoadingViolationError
#
# ==== Parameters
Expand All @@ -629,12 +631,13 @@ def strict_loading?
#
# user = User.first
# user.strict_loading!(false) # => false
# user.comments
# => #<ActiveRecord::Associations::CollectionProxy>
# user.address.city # => "Tatooine"
# user.comments.to_a # => [#<Comment:0x00...]
#
# user.strict_loading!(mode: :n_plus_one_only)
# user.address.city # => "Tatooine"
# user.comments
# user.comments.to_a # => [#<Comment:0x00...]
# user.comments.first.ratings.to_a
# => ActiveRecord::StrictLoadingViolationError
def strict_loading!(value = true, mode: :all)
unless [:all, :n_plus_one_only].include?(mode)
Expand Down
27 changes: 26 additions & 1 deletion guides/source/active_record_querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -1754,15 +1754,40 @@ some associations. To make sure no associations are lazy loaded you can enable

By enabling strict loading mode on a relation, an
`ActiveRecord::StrictLoadingViolationError` will be raised if the record tries
to lazily load an association:
to lazily load any association:

```ruby
user = User.strict_loading.first
user.address.city # raises an ActiveRecord::StrictLoadingViolationError
user.comments.to_a # raises an ActiveRecord::StrictLoadingViolationError
```

[`strict_loading`]: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-strict_loading

### `strict_loading!`

We can also enable strict loading on the record itself by calling [`strict_loading!`][]:

```ruby
user = User.first
user.strict_loading!
user.address.city # raises an ActiveRecord::StrictLoadingViolationError
user.comments.to_a # raises an ActiveRecord::StrictLoadingViolationError
```

`strict_loading!` also takes a `:mode` argument. Setting it to `:n_plus_one_only`
will only raise an error if an association that will lead to an N + 1 query is
lazily loaded:

```ruby
user.strict_loading!(mode: :n_plus_one_only)
user.address.city # => "Tatooine"
user.comments.to_a # => [#<Comment:0x00...]
user.comments.first.likes.to_a # raises an ActiveRecord::StrictLoadingViolationError
```

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

Scopes
------

Expand Down

0 comments on commit 6f27e92

Please sign in to comment.