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

Opting out of strict loading on a per-record base #41181

Merged
merged 1 commit into from Jan 21, 2021
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
22 changes: 22 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,25 @@
* Allow to opt-out of `strict_loading` mode on a per-record base.

This is useful when strict loading is enabled application wide or on a
model level.

```ruby
class User < ApplicationRecord
has_many :articles, strict_loading: true
end

user = User.first
user.articles
# => ActiveRecord::StrictLoadingViolationError

user = User.first
user.stict_loading!(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.strict_loading!(false)

user.articles
# => #<ActiveRecord::Associations::CollectionProxy>
```

*Ayrton De Craene*

* Add `FinderMethods#sole` and `#find_sole_by` to find and assert the
presence of exactly one record.

Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -654,8 +654,8 @@ def strict_loading?
# user.strict_loading!
# user.comments.to_a
# => ActiveRecord::StrictLoadingViolationError
def strict_loading!
@strict_loading = true
def strict_loading!(value = true)
@strict_loading = value
end

# Marks this record as read only.
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/strict_loading_test.rb
Expand Up @@ -24,6 +24,13 @@ def test_strict_loading!
assert_raises ActiveRecord::StrictLoadingViolationError do
developer.audit_logs.to_a
end

developer.strict_loading!(false)
assert_not_predicate developer, :strict_loading?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assertion that developer.audit_logs doesn't raise? I think we should also have a test showing that it also works if the strict loading is defined in the relation. See strict_loading_audit_logs below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8223023


assert_nothing_raised do
developer.audit_logs.to_a
end
end

def test_strict_loading
Expand Down Expand Up @@ -147,6 +154,10 @@ def test_eager_load_audit_logs_are_strict_loading_because_parent_is_strict_loadi
dev = Developer.eager_load(:strict_loading_audit_logs).first

assert dev.strict_loading_audit_logs.all?(&:strict_loading?), "Expected all audit logs to be strict_loading"

dev = Developer.eager_load(:strict_loading_audit_logs).strict_loading(false).first

assert dev.audit_logs.none?(&:strict_loading?), "Expected no audit logs to be strict_loading"
end

def test_eager_load_audit_logs_are_strict_loading_because_parent_is_strict_loading
Expand All @@ -160,6 +171,11 @@ def test_eager_load_audit_logs_are_strict_loading_because_parent_is_strict_loadi

assert_predicate dev, :strict_loading?
assert dev.audit_logs.all?(&:strict_loading?), "Expected all audit logs to be strict_loading"

dev = Developer.eager_load(:audit_logs).strict_loading(false).first

assert_not_predicate dev, :strict_loading?
assert dev.audit_logs.none?(&:strict_loading?), "Expected no audit logs to be strict_loading"
end

def test_eager_load_audit_logs_are_strict_loading_because_it_is_strict_loading_by_default
Expand Down