# Named\_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return

**URL:** <https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171>\
**Category:** Rails\
**Created:** [January 20, 2009, 2:45am UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171 "2009-01-20T02:45:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 2:45am UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/1 "2009-01-20T02:45:59Z")

</div>

Hi,

I’ve just discovered the “named\_scope” method which seems great. I’m  
wondering there is any reason why you wouldn’t always use a named scope  
(to  
get the advantages), over a custom method on your model to return an  
array  
of records?

That is using:

class Shirt \< ActiveRecord::Base  
named\_scope :red, :conditions =\> {:color =\> ‘red’}  
end

instead of

class Shirt \< ActiveRecord::Base  
def red  
Shirt.find\_all\_by\_color(‘red’)  
end  
end

Comments?

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 3:32am UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/2 "2009-01-20T03:32:54Z")

</div>

Greg H. wrote:

> Hi,
> 
> I’ve just discovered the “named\_scope” method which seems great. I’m  
> wondering there is any reason why you wouldn’t always use a named scope  
> (to  
> get the advantages), over a custom method on your model to return an  
> array  
> of records?
> 
> That is using:
> 
> class Shirt \< ActiveRecord::Base  
> named\_scope :red, :conditions =\> {:color =\> ‘red’}  
> end
> 
> instead of
> 
> class Shirt \< ActiveRecord::Base  
> def red  
> Shirt.find\_all\_by\_color(‘red’)  
> end  
> end  
> Well, this second case is kind of redundant since you are effectively  
> creating two methods that do exactly the same thing. The defined method  
> “red” and the dynamically created method (via method\_missing)  
> “find\_all\_by\_color(‘red’)”

But, realizing this is a contrived example there are advantages to using  
the named scopes because name scopes can be chained:  
large\_red\_shirts = Shirt.large.red

At least as I understand named\_scope.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 21, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/3 "2009-01-21T12:25:39Z")

</div>

(bump)

On Tue, Jan 20, 2009 at 11:45 AM, Greg H. \<  
removed\_email\_address@domain.invalid\> wrote:

> ```
> named_scope :red, :conditions => {:color => 'red'}
> 
> ```

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 21, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/4 "2009-01-21T12:43:51Z")

</div>

On Tue, Jan 20, 2009 at 3:45 AM, Greg H. \<  
removed\_email\_address@domain.invalid\> wrote:

> ```
> named_scope :red, :conditions => {:color => 'red'}
> 
> ```
> 
> >

The only reason to use a custom method instead of a named\_scope is when  
you’re doing more than querying the db or there’s something named\_scopes  
can’t handle.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 21, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/5 "2009-01-21T21:16:31Z")

</div>

ok - so if you had the time, updating all your custom finder methods to  
named\_scope would give you more flexibility then. I was just wanting to  
check if there were any negatives or gotchas to named\_scope’s

On Wed, Jan 21, 2009 at 9:42 PM, Andrew T. \<  
removed\_email\_address@domain.invalid\> wrote:

> > That is using:
> > 
> > Greg  
> > –  
> > Andrew T.  
> > [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> > [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 22, 2009, 10:02am UTC](https://www.ruby-forum.com/t/named-scopes-any-reason-why-you-should-not-alway-use-a-named-scope-c-f-a-custom-method-to-return/158171/6 "2009-01-22T10:02:22Z")

</div>

On Wed, Jan 21, 2009 at 10:15 PM, Greg H. \<  
removed\_email\_address@domain.invalid\> wrote:

> ok - so if you had the time, updating all your custom finder methods to  
> named\_scope would give you more flexibility then. I was just wanting to  
> check if there were any negatives or gotchas to named\_scope’s

It would definitely give you more flexibility because of the ability to  
chain together named\_scopes.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain
