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

Don't cache scope_for_create #29814

Merged
merged 1 commit into from Jul 16, 2017
Merged

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Jul 16, 2017

I investigated where scope_for_create is reused in tests with the
following code:

--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -590,6 +590,10 @@ def where_values_hash(relation_table_name = table_name)
     end

     def scope_for_create
+      if defined?(@scope_for_create) && @scope_for_create
+        puts caller
+        puts "defined"
+      end
       @scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys)
     end

It was hit only test_scope_for_create_is_cached. This means that
scope_for_create will not be reused in normal use cases. So we can
remove caching scope_for_create to respect changing where_clause and
create_with_value.

I investigated where `scope_for_create` is reused in tests with the
following code:

```diff
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -590,6 +590,10 @@ def where_values_hash(relation_table_name = table_name)
     end

     def scope_for_create
+      if defined?(@scope_for_create) && @scope_for_create
+        puts caller
+        puts "defined"
+      end
       @scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys)
     end
```

It was hit only `test_scope_for_create_is_cached`. This means that
`scope_for_create` will not be reused in normal use cases. So we can
remove caching `scope_for_create` to respect changing `where_clause` and
`create_with_value`.
@kaspth kaspth merged commit 919b55d into rails:master Jul 16, 2017
@kamipo kamipo deleted the dont_cache_scope_for_create branch July 16, 2017 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants