Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve strict_loading documentation [ci-skip] #49329

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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