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.
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---