From 0130c17476b754aea20faf6914686c5d4c7086b9 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 9 Nov 2012 10:35:39 +0000 Subject: Relations built off collection associations with an unsaved owner should be null relations For example, the following should not run any query on the database: Post.new.comments.where(body: 'omg').to_a # => [] Fixes #5215. --- activerecord/lib/active_record/associations/collection_proxy.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index e73f940334..536e108f0e 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -835,8 +835,9 @@ module ActiveRecord # Returns a Relation object for the records in this association def scope association = @association - - @association.scope.extending! do + scope = @association.scope + scope.none! if @association.owner.new_record? + scope.extending! do define_method(:proxy_association) { association } end end -- cgit v1.2.3