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?
Should I use a function instead? For example:
def find_by_genre(genre)
self.where([''genres.name = ?'', genre).includes({:artist
=> {:user
=> :genre}})
end
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Oct 9, 4:24 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Please quote when replying. Christian Fazzini 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?A named_scope is a method, or as near as matters. Use whichever is easier to implement and otherwise more suitable to your use case. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 8, 9:30 pm, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sometimes if I need to do a calculation before I call a named scope or if I want
to make a different query based on the input, I do something like this:
class Media < AR:Base
def dvds(name = "")
if name.blank?
Media.scoped({})
else
name.downcase!
Media.scoped(:conditions => {:name => name})
end
end
end
In either case, it''s still chainable. This is Rails2 code, but
I''m sure you could do something similar with Rails3.
Luke
On 2010-10-08, at 2:25 PM, Frederick Cheung wrote:
>
>
> On Oct 8, 9:30 pm, Christian Fazzini
<christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> 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
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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
>>> mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org
>>> --
>>> 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 October 2010 00:44, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sometimes if I need to do a calculation before I call a named scope or if I want to make a different query based on the input, I do something like this: > > class Media < AR:Base > def dvds(name = "") > if name.blank? > Media.scoped({}) > else > name.downcase! > Media.scoped(:conditions => {:name => name}) > end > end > endCould that just be Media.scoped( :conditions => {:name => name.downcase!} unless name.blank? ) or something similar, I have not tried it. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Colin, yes something like what you''ve proposed would work. This is
the most concise syntax I could come up with to get your suggestion working.
Media.scoped(:conditions => (name.blank? ? {:name => name.downcase!} :
{}))
My suggestion (below) was to illustrate that you could have something that looks
like a named scope (dvds) actually be a method. At some point the logic you need
to apply may not fit nicely into a single named_scope call and I find this to be
a good way to handle these situations.
Luke
On 2010-10-09, at 2:28 PM, Colin Law wrote:
> On 9 October 2010 00:44, Luke Cowell
<lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> Sometimes if I need to do a calculation before I call a named scope or
if I want to make a different query based on the input, I do something like
this:
>>
>> class Media < AR:Base
>> def dvds(name = "")
>> if name.blank?
>> Media.scoped({})
>> else
>> name.downcase!
>> Media.scoped(:conditions => {:name => name})
>> end
>> end
>> end
>
> Could that just be
> Media.scoped( :conditions => {:name => name.downcase!} unless
name.blank? )
> or something similar, I have not tried it.
>
> Colin
>
> --
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.