Greg Hauptmann
2009-Jan-20 01:45 UTC
named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return limited results)
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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2009-Jan-20 02:32 UTC
Re: named_scopes - any reason why you should not alway use a
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 > endWell, 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2009-Jan-21 11:24 UTC
Re: named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return limited results)
(bump) On Tue, Jan 20, 2009 at 11:45 AM, Greg Hauptmann < greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > Comments? > > > > -- > Greg > http://blog.gregnet.org/ > > >-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Timberlake
2009-Jan-21 11:42 UTC
Re: named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return limited results)
On Tue, Jan 20, 2009 at 3:45 AM, Greg Hauptmann < greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > Comments? > > > > -- > Greg > http://blog.gregnet.org/ > > > > > >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2009-Jan-21 20:15 UTC
Re: named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return limited results)
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-642hCh26+Dt3UeSHeRwt+FaTQe2KTcn/@public.gmane.org> wrote:> On Tue, Jan 20, 2009 at 3:45 AM, Greg Hauptmann < > greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >> >> >> Comments? >> >> >> >> -- >> Greg >> http://blog.gregnet.org/ >> >> >> >> >> > 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 > > > >-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Timberlake
2009-Jan-22 09:01 UTC
Re: named_scopes - any reason why you should not alway use a named scope (c.f. a custom method to return limited results)
On Wed, Jan 21, 2009 at 10:15 PM, Greg Hauptmann < greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---