On Oct 8, 9:30 pm, Christian Fazzini <[email protected]>
wrote:
> If thats the case, when should one use a named scope over a method?
>
> For example:
>
> Product.find_by_category(category).
>
> find_by_category can either be a named scope or a method in the
> Product model. I was under the impression that named scopes cant
> accept an attribute and this was the main difference. Apparently, it
> can!
>
> So when does one use named scopes over methods?

(Nitpicking: a call to named_scope (or scope in rails 3) does just
define a method of a particular form)
Scopes are chainable which can be very handy. On the other hand there
is only so much a scope can do - scopes boil down to a single query to
the database, so if you need to do more, then you'll probably want to
wrap up the extra work in a method.

Fred
>
> On Oct 9, 4:24 am, Marnen Laibow-Koser <[email protected]> wrote:
>
>
>
> > Christian Fazzini wrote:
> > > In my Video model i have:
>
> > > class Video < Media
> > >   scope :genre, where(['genres.name = ?',
> > > params[:genre].capitalize]).includes({:artist => {:user => :genre}})
> > >   ....
> > > end
>
> > > As you see above, params[:genre].capitalize is a dynamic value. How do
> > > I pass a value when I call Video.genre?
>
> > > Or are we not allowed to pass attributes/values to named scopes?
>
> > Read the AR rdoc on named_scopes.  It explains this quite clearly.
> > (Hint: lambdas are involved.)
>
> > Best,
> > --
> > Marnen Laibow-Koserhttp://www.marnen.org
> > [email protected]
> > --
> > Posted viahttp://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.

Reply via email to