# Is AR find(:all) a 100% named\_scope?

**URL:** <https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680>\
**Category:** Rails\
**Created:** [November 9, 2009, 12:36pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680 "2009-11-09T12:36:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joahking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@joahking](https://www.ruby-forum.com/u/joahking)\
**Post date:** [November 9, 2009, 12:36pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680/1 "2009-11-09T12:36:46Z")

</div>

hi there,  
I have a method in a model that concatenates named\_scopes at later  
points in  
the method if certain conditions given. The concatenations go ok when  
all  
that concatenation is done with named\_scopes, but it blows when  
ActiveRecord.all is used in it

see it in the gist: [is\_find\_all\_a\_named\_scope?.rb · GitHub](http://gist.github.com/229884)

I could of course write that in some other way, but the question would  
be:  
could AR.all be turned into a NamedScope and not an Array?

any ideas? in the mean time I will refactor my code and make my specs  
happy  
🙂

cheers,  
joaquin

---

<div class="post-metadata">

**Author:** ![joahking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@joahking](https://www.ruby-forum.com/u/joahking)\
**Post date:** [November 9, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680/2 "2009-11-09T12:47:19Z")

</div>

```auto
gs = if privates
            self.privates # with named_scopes everything runs fine
          else
            self.all # here we won't get a NamedScope, instead it'll
be an Array
          end
```

Try:

```auto
   gs = if privates
          self.privates
        else
          self
        end
```

åœ¨ 2009/11/9 ä¸‹åˆ 7:35 æ™‚ï¼Œ Joaquin Rivera P. å¯«åˆ°ï¼š

> any ideas? in the mean time I will refactor my code and make my  
> specs happy 🙂
> 
> cheers,  
> joaquin
> 
> >

HeChian H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![joahking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@joahking](https://www.ruby-forum.com/u/joahking)\
**Post date:** [November 9, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680/3 "2009-11-09T12:55:33Z")

</div>

On 09 Nov 2009, at 12:35, Joaquin Rivera P. wrote:

> any ideas? in the mean time I will refactor my code and make my  
> specs happy 🙂

Well, the implementation of AR.all is not a named\_scope as you  
obviously found out:

```
   # This is an alias for find(:all). You can pass in all the

```

same arguments to this method as you can  
# to find(:all)  
def all(\*args)  
find(:all, \*args)  
end

A quick google search revealed this blog post:  
[http://www.neeraj.name/blog/articles/871-fixing-activerecord-base-all-and-making-it-friendlier-to-named\_scope](http://www.neeraj.name/blog/articles/871-fixing-activerecord-base-all-and-making-it-friendlier-to-named_scope)

It outlines both the problem and provides a solution. I do agree that  
since AR is clearly pushing chained scopes as a way to build queries,  
a named scope implementation would be better. But named scopes have  
quite a bit of work ahead, since arguments don’t get overridden if a  
subsequent scope defines them differently etc. Example: if you  
make :active =\> true the default scope and then make a named\_scope  
of :inactive, defined as :active =\> false, you’ll get a contradictory  
query.

On the other hand, Rails is open source and well thought out and  
tested contributions do make it into the core, so it’s only a matter  
of someone stepping up to get it sorted. Other ORM options have  
already tackled these problems more elegantly afaik, like Datamapper  
and Arel.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![joahking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@joahking](https://www.ruby-forum.com/u/joahking)\
**Post date:** [November 9, 2009, 1:16pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680/4 "2009-11-09T13:16:03Z")

</div>

hi all,  
thanks for you reply,

2009/11/9 Peter De Berdt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I could of course write that in some other way, but the question would be:  
> arguments to this method as you can  
> # to find(:all)  
> def all(\*args)  
> find(:all, \*args)  
> end
> 
> A quick google search revealed this blog post:  
> [http://www.neeraj.name/blog/articles/871-fixing-activerecord-base-all-and-making-it-friendlier-to-named\_scope](http://www.neeraj.name/blog/articles/871-fixing-activerecord-base-all-and-making-it-friendlier-to-named_scope)

Peter yep, nice post, only for the moment I don’t feel like patching  
AR::Base, and I get lots of warnings when running my specs:

/home/joahking/dev/rails/tucamon/app/models/group.rb:44: warning:  
multiple  
values for a block parameter (0 for 1)  
from  
/home/joahking/dev/rails/tucamon/vendor/rails/activerecord/lib/active\_record/named\_scope.rb:92

line 44: named\_scope :all, lambda { |args| args ||= {} }

I’ll go with HeChian H. idea, it ended like this:  
gs = if PrivacyLevels.include? privacy  
self.send privacy  
else  
self # AR itself  
end

```
if name
  gs.named_like name
else
  gs.all # and all again
end

```

I know it’s not a very general solution, but it keeps all them specs  
green  
🙂

I have to move on given my deadlines, but if I stumble again I’ll give a  
second thought

thanks for your responses,  
joaquin

It outlines both the problem and provides a solution. I do agree that  
since

> problems more elegantly afaik, like Datamapper and Arel.
> 
> Best regards
> 
> Peter De Berdt
> 
> >

–

> **[Least Significant Bit](http://www.least-significant-bit.com)**
>
> The blog of Joaquin Rivera Padron (@joahking). Here I share my thoughts about programming, architecture, scalability, Open Source, Ruby, Testing, BDD, linux and more.

---

<div class="post-metadata">

**Author:** ![joahking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@joahking](https://www.ruby-forum.com/u/joahking)\
**Post date:** [November 9, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/is-ar-find-all-a-100-named-scope/178680/5 "2009-11-09T12:56:43Z")

</div>

On Nov 9, 11:35 am, Joaquin Rivera P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ActiveRecord.all is used in it
> 
> see it in the gist:[is\_find\_all\_a\_named\_scope?.rb · GitHub](http://gist.github.com/229884)
> 
> I could of course write that in some other way, but the question would be:  
> could AR.all be turned into a NamedScope and not an Array?

It could be interesting to do this. For now instead of doing self.all  
you could try self.all.scoped({})

Fred
