Forum: Ruby on Rails named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2009-01-20 02:46
(Received via mailing list)
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/
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-01-20 03:32
Greg Hauptmann 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.
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2009-01-21 12:25
(Received via mailing list)
(bump)

On Tue, Jan 20, 2009 at 11:45 AM, Greg Hauptmann <
greg.hauptmann.ruby@gmail.com> wrote:

>     named_scope :red, :conditions => {:color => 'red'}
>
>
--
Greg
http://blog.gregnet.org/
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-21 12:43
(Received via mailing list)
On Tue, Jan 20, 2009 at 3:45 AM, Greg Hauptmann <
greg.hauptmann.ruby@gmail.com> 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2009-01-21 21:16
(Received via mailing list)
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 Timberlake <
andrew@andrewtimberlake.com> wrote:

>> That is using:
>>
>> Greg
> --
> Andrew Timberlake
> http://ramblingsonrails.com
> http://www.linkedin.com/in/andrewtimberlake
>
> "I have never let my schooling interfere with my education" - Mark Twain
>
> >
>


--
Greg
http://blog.gregnet.org/
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-22 10:02
(Received via mailing list)
On Wed, Jan 21, 2009 at 10:15 PM, Greg Hauptmann <
greg.hauptmann.ruby@gmail.com> 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
This topic is locked and can not be replied to.