Hi, I would like to return a combination of named scopes in a named scope : For example, I have a named scope filter and I want to add a named scope eval_filters like Product.eval_filters([''x'',''y'',''z'']) is equivalent to Product.filter(''x'').filter(''y'').filter(''z'') Anybody know how can I define the named_scope eval_filters to do that ? Thanks adrien :) -- 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.
Adrien Nom wrote:> Hi, > > I would like to return a combination of named scopes in a named > scope : > > For example, I have a named scope filter and I want to add a named > scope eval_filters like > > Product.eval_filters([''x'',''y'',''z'']) is equivalent to > Product.filter(''x'').filter(''y'').filter(''z'')From http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/, you could do something like scope :eval_filters, lambda { |fltr| fltr.inject(scoped) { |combined_scope, flt| combined_scope.where("id = ?", flt) }} Thanks Raja -- 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 Sep 23, 9:29 pm, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I would like to return a combination of named scopes in a named > scope : > > For example, I have a named scope filter and I want to add a named > scope eval_filters like > > Product.eval_filters([''x'',''y'',''z'']) is equivalent to > Product.filter(''x'').filter(''y'').filter(''z'') >Well it wouldn''t be a scope strictly speaking, but it would return a scope and I believe you could call it on a scope: def self.eval_filters(filters) filters.inject(self) { |scope, filter| scope.send filter} end> Anybody know how can I define the named_scope eval_filters to do > that ? > > Thanks > > adrien :)-- 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.
Perfect solution ! Thanks a lot :) adrien On Sep 24, 9:35 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 23, 9:29 pm, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I would like to return a combination of named scopes in a named > > scope : > > > For example, I have a named scope filter and I want to add a named > > scope eval_filters like > > > Product.eval_filters([''x'',''y'',''z'']) is equivalent to > > Product.filter(''x'').filter(''y'').filter(''z'') > > Well it wouldn''t be a scope strictly speaking, but it would return a > scope and I believe you could call it on a scope: > > def self.eval_filters(filters) > filters.inject(self) { |scope, filter| scope.send filter} > end > > > > > Anybody know how can I define the named_scope eval_filters to do > > that ? > > > Thanks > > > adrien :)-- 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.
Finally I still have a problem : I used this definition for my "pseudo-named scope" : def self.eval_filters(filters) filters.inject(self) { |scope, filter| scope.send filter} end That works fine when I give some filters to the function but when filters is an empty array, I lost the previous name scope. Example : Category.first.products.eval_filters("public_domain") -----> OK ! Category.first.products.eval_filters(nil) ------> return Product.all When I use the debugger in the function, self is always an instance of the class Product. Any help please ? Thanks a lot adrien NB: I use Rails 2.3.8 On Sep 24, 11:01 am, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Perfect solution ! > Thanks a lot :) > > adrien > > On Sep 24, 9:35 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Sep 23, 9:29 pm, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > > I would like to return a combination of named scopes in a named > > > scope : > > > > For example, I have a named scope filter and I want to add a named > > > scope eval_filters like > > > > Product.eval_filters([''x'',''y'',''z'']) is equivalent to > > > Product.filter(''x'').filter(''y'').filter(''z'') > > > Well it wouldn''t be a scope strictly speaking, but it would return a > > scope and I believe you could call it on a scope: > > > def self.eval_filters(filters) > > filters.inject(self) { |scope, filter| scope.send filter} > > end > > > > Anybody know how can I define the named_scope eval_filters to do > > > that ? > > > > Thanks > > > > adrien :)-- 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.
On Sep 28, 9:38 am, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That works fine when I give some filters to the function but when > filters is an empty array, I lost the previous name scope. > Example : > > Category.first.products.eval_filters("public_domain") -----> OK ! > Category.first.products.eval_filters(nil) ------> return Product.all > > When I use the debugger in the function, self is always an instance of > the class Product. >yeah, that will happen. Best thing is to call a trivial scope eg scoped({}) Fred -- 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.
Works well ! I replace filters.inject(self) { |scope, filter| scope.send filter} by filters.inject(scoped) { |scope, filter| scope.send filter} Thanks :) adrien On Sep 28, 12:50 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 28, 9:38 am, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > That works fine when I give some filters to the function but when > > filters is an empty array, I lost the previous name scope. > > Example : > > > Category.first.products.eval_filters("public_domain") -----> OK ! > > Category.first.products.eval_filters(nil) ------> return Product.all > > > When I use the debugger in the function, self is always an instance of > > the class Product. > > yeah, that will happen. Best thing is to call a trivial scope eg > scoped({}) > > Fred-- 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.