# Anonymous scopes hitting database

**URL:** <https://www.ruby-forum.com/t/anonymous-scopes-hitting-database/147059>\
**Category:** Rails\
**Created:** [August 28, 2008, 4:53am UTC](https://www.ruby-forum.com/t/anonymous-scopes-hitting-database/147059 "2008-08-28T04:53:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdevine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jdevine](https://www.ruby-forum.com/u/jdevine)\
**Post date:** [August 28, 2008, 4:53am UTC](https://www.ruby-forum.com/t/anonymous-scopes-hitting-database/147059/1 "2008-08-28T04:53:02Z")

</div>

From what I can understand, I should be able to treat an anonymous  
scope as an intermediary object with which to build queries on the  
fly.

Following the procedure in Railscast episode 112 (http://  
[#112 Anonymous Scopes - RailsCasts](http://railscasts.com/episodes/112)), I try the following (in script/console):

a = Production.scoped({})

Hitting return results in a call to the database and all (hundreds of  
thousands) records returned.

Is this the expected behavior?

I was hoping to use this to add various scopes according to the search  
parameters requested, but I can’t afford to do full table scans on  
each step.

What could I be doing wrong?

Thanks for any ideas,

John

---

<div class="post-metadata">

**Author:** ![jdevine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jdevine](https://www.ruby-forum.com/u/jdevine)\
**Post date:** [August 28, 2008, 9:49am UTC](https://www.ruby-forum.com/t/anonymous-scopes-hitting-database/147059/2 "2008-08-28T09:49:06Z")

</div>

On Aug 28, 3:52 am, JDevine [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thousands) records returned.
> 
> Is this the expected behavior?
> 
> I was hoping to use this to add various scopes according to the search  
> parameters requested, but I can’t afford to do full table scans on  
> each step.
> 
> What could I be doing wrong?

The console is fooling you: it’s trying to display a, and trying to  
display it causes the scope to be loaded.  
if you did a - Production.scoped({}); false  
(so what the console displays is just false) then you should be ok

Fred

---

<div class="post-metadata">

**Author:** ![jdevine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@jdevine](https://www.ruby-forum.com/u/jdevine)\
**Post date:** [August 28, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-hitting-database/147059/3 "2008-08-28T17:58:38Z")

</div>

That’s very good news. I’m just not up for a dive into the internals  
today.

Thanks Frederick!

On Aug 28, 2:48 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
