Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CollectionProxy#pluck issues no query for a new_record? owner

Fixes #8102.

I couldn't find a nicer way to deal with this than delegate the call to
 #scope, which will be a NullRelation when we want it to be.
  • Loading branch information...
commit 11b846eefb1e7128ae9c63035f6d559d50bd4563 1 parent aae4f35
@jonleighton jonleighton authored
View
4 activerecord/lib/active_record/associations/collection_proxy.rb
@@ -965,6 +965,10 @@ def reload
proxy_association.reload
self
end
+
+ def pluck(column_names)
+ scope.pluck(column_names)
+ end
end
end
end
View
1  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1655,6 +1655,7 @@ def test_collection_association_with_private_kernel_method
assert_no_queries do
assert_equal [], post.comments
assert_equal [], post.comments.where(body: 'omg')
@sobrinho
sobrinho added a note

@jonleighton just to be sure, calling where without to_a in that case will try to trigger the query?

I mean, the where method returns a relation, just comparing it using the assert_equal will try to make the query?

@jonleighton Collaborator

Relations have a #to_ary method, so [] == Relation.new(..) will convert the Relation to an array.

@sobrinho
sobrinho added a note

@jonleighton makes sense, thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ assert_equal [], post.comments.pluck(:body)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.