title: Why won't these nested unscoped blocks work to remove default_scope in Rails 4 when they work in Rails 3?
body: <p><strong>My question</strong></p>
  
  <p>It seems like Rails 4 is ignoring nested unscoped blocks (whereas they were ok in Rails 3). I've been Googling like crazy and can't find anything indicating a change here. Any ideas how I can get this working in Rails 4?</p>
  
  <p><strong>What I'm doing</strong></p>
  
  <p>I'm using default_scope for multi-tenancy as shown in <a href="http://railscasts.com/episodes/388-multitenancy-with-scopes" rel="nofollow">#388 Multitenancy with Scopes</a>. Some admins will be admin of multiple tenants, and I want to show them aggregated data in a report. I'm doing this by using <a href="http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/unscoped" rel="nofollow">unscoped blocks</a>. I'm also preloading associated objects because 1) It's more efficient and 2) I need to get all the associated objects in one place so I don't have to keep using unscoped blocks in my views. To preload the associated objects, I'm using nested unscoped blocks.</p>
  
  <p>I had this working in my app on Rails 3.2.17, but now that I've upgraded to Rails 4.0.1.rc1, it no longer works. </p>
  
  <p><strong>A simple example to illustrate the difference</strong></p>
  
  <p>Below, I'll show what I'm getting in the Console. This example is much simpler than what I actually want to do, but I think it's the easiest way to show what's going on.</p>
  
  <pre><code>Loading development environment (Rails 3.2.17)
  1.9.3-p374 :001 &gt; s = nil
     =&gt; nil 
  1.9.3-p374 :002 &gt; Submission.unscoped { Checklist.unscoped { s = Submission.preload(:checklist).find(3269) }}
    Submission Load (27.8ms)  SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1  [["id", 3269]]
    Checklist Load (0.6ms)  SELECT "checklists".* FROM "checklists" WHERE "checklists"."id" IN (17)
   =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
  1.9.3-p374 :003 &gt; s
   =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
  1.9.3-p374 :004 &gt; s.checklist
   =&gt; #&lt;Checklist id: 17, name: "Open", description: "Chores Required To Open Store Front", creator_id: 2, created_at: "2013-09-23 21:55:23", updated_at: "2013-09-23 21:55:23", archived: false, archived_at: nil, ancestry: nil, tenant_id: 2&gt;
  </code></pre>
  
  <p>So, I'm loading a submission and preloading its associated checklist. Then I can confirm that both the submission and its associated checklist are available.</p>
  
  <p>If I switch to my Rails 4 environment (both working on the same database), here is what I see:</p>
  
  <pre><code> Loading development environment (Rails 4.1.0.rc1)
   1.9.3-p374 :001 &gt; s = nil
    =&gt; nil 
   1.9.3-p374 :002 &gt; Submission.unscoped { Checklist.unscoped { s = Submission.preload(:checklist).find(3269) }}
     Submission Load (0.4ms)  SELECT  "submissions".* FROM "submissions"  WHERE "submissions"."id" = $1 LIMIT 1  [["id", 3269]]
     Checklist Load (0.6ms)  SELECT "checklists".* FROM "checklists"  WHERE "checklists"."tenant_id" IS NULL AND "checklists"."id" IN (17)
    =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
   1.9.3-p374 :003 &gt; s
    =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
   1.9.3-p374 :004 &gt; s.checklist
    =&gt; nil
  </code></pre>
  
  <p>I'm running exactly the same code, but only the Submission is available - its associated checklist is nil.</p>
  
  <p>FWIW, I can duplicate this in Rails 3.2.17 if I run this in the Console. <strong>NOTE: The difference here is I'm only using a Submission.unscoped {} block and not nesting the Checklist.unscoped {} block.</strong></p>
  
  <pre><code>  Loading development environment (Rails 3.2.17)
    1.9.3-p374 :001 &gt; s = nil
     =&gt; nil 
    1.9.3-p374 :002 &gt; Submission.unscoped { s = Submission.preload(:checklist).find(3269) }
      Submission Load (3.6ms)  SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1  [["id", 3269]]
      Checklist Load (0.4ms)  SELECT "checklists".* FROM "checklists" WHERE "checklists"."tenant_id" IS NULL AND "checklists"."id" IN (17)
     =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
    1.9.3-p374 :003 &gt; s
     =&gt; #&lt;Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank"&gt; 
    1.9.3-p374 :004 &gt; s.checklist
     =&gt; nil
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I went digging into the Rails source and I can see that the "unscoped" method is the same in 3.2-stable as current master, BUT that method calls "relation.scoping" and that method is substantially different between the two branches. I'm not sure if there's been a bug introduced, or if the expected behavior has changed.</p>
  
  <p>For reference:</p>
  
  <p><a href="https://github.com/rails/rails" rel="nofollow">current master</a></p>
  
  <pre><code>def unscoped
    block_given? ? relation.scoping { yield } : relation
  end
  </code></pre>
  
  <p><strong>relation.scoping</strong></p>
  
  <pre><code>def scoping
    previous, klass.current_scope = klass.current_scope, self
    yield
  ensure
    klass.current_scope = previous
  end
  </code></pre>
  
  <p><strong>current_scope</strong></p>
  
  <pre><code>def current_scope #:nodoc:
    ScopeRegistry.value_for(:current_scope, base_class.to_s)
  end
  </code></pre>
  
  <p><a href="https://github.com/rails/rails/tree/3-2-stable" rel="nofollow">3.2-stable</a></p>
  
  <pre><code>def unscoped #:nodoc:
    block_given? ? relation.scoping { yield } : relation
  end
  </code></pre>
  
  <p><strong>relation.scoping</strong></p>
  
  <pre><code>def scoping
    @klass.with_scope(self, :overwrite) { yield }
  end
  </code></pre>
  
  <p>Link to <a href="https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/scoping.rb" rel="nofollow">with_scope</a> method...</p>
  
  <pre><code>def with_scope
    # Pretty long and involved method
    # See Line 60 in the linked doc
  end
  </code></pre>