From 5f759ff063b8ed054cdde0ed4f3593ef89fe59b7 Mon Sep 17 00:00:00 2001
From: Jon Leighton <j@jonathanleighton.com>
Date: Fri, 18 Jan 2013 11:25:00 +0000
Subject: CollectionProxy should be default scoped

Fixes #8795
---
 activerecord/lib/active_record/associations/collection_proxy.rb    | 1 +
 activerecord/test/cases/associations/has_many_associations_test.rb | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index 33dce58982..e93e700c93 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -33,6 +33,7 @@ module ActiveRecord
       def initialize(klass, association) #:nodoc:
         @association = association
         super klass, klass.arel_table
+        self.default_scoped = true
         merge! association.scope(nullify: false)
       end
 
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 28400970fb..2aa3ecb108 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1712,4 +1712,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
       assert_equal 0,  post.comments.count
     end
   end
+
+  test "collection proxy respects default scope" do
+    author = authors(:mary)
+    assert !author.first_posts.exists?
+  end
 end
-- 
cgit v1.2.3