Is it possible to have OR conditions using named scope ??
For e.g. Gather users based on following or conditions.
1. active is one named scope which finds all users whose state is active
2. OR user id is in 1,2,3
This condition can be written in only one named scope as
named_scope :active_or_explicit, lambda{ |ids|
{ :conditions => [''state = ? OR id in (?)'',
''active'', [1,2,3]] }
}
Is it possible to have two named scopes that can be joined using OR condition.
Please let me know your views...
--
Sandip
---
www.funonrails.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.
Sandip Ransing wrote:> Is it possible to have OR conditions using named scope ?? > For e.g. Gather users based on following or conditions. > 1. active is one named scope which finds all users whose state is active > 2. OR user id is in 1,2,3 > > This condition can be written in only one named scope as > > named_scope :active_or_explicit, lambda{ |ids| > { :conditions => [''state = ? OR id in (?)'', ''active'', [1,2,3]] } > } > > Is it possible to have two named scopes that can be joined using OR > condition. >Goodness, I hope not. That would be a terrible misuse of named_scopes.> Please let me know your views...Please see http://groups.google.com/group/rubyonrails-talk/msg/7789576953707b2e for some of my reasoning.> > -- > Sandip > > --- > www.funonrails.comBest, -- 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.
Marnen thx for reply ! Actually I wanted to add "OR" condition to acts_as_mappable finders, So that result-set will contain my own preferences too...but now it looks like for this purpose i may either need to mess up with library of rails_geokit plugin or override source of named_scope :( On 8/18/10, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sandip Ransing wrote: >> Is it possible to have OR conditions using named scope ?? >> For e.g. Gather users based on following or conditions. >> 1. active is one named scope which finds all users whose state is active >> 2. OR user id is in 1,2,3 >> >> This condition can be written in only one named scope as >> >> named_scope :active_or_explicit, lambda{ |ids| >> { :conditions => [''state = ? OR id in (?)'', ''active'', [1,2,3]] } >> } >> >> Is it possible to have two named scopes that can be joined using OR >> condition. >> > > Goodness, I hope not. That would be a terrible misuse of named_scopes. > >> Please let me know your views... > > Please see > http://groups.google.com/group/rubyonrails-talk/msg/7789576953707b2e for > some of my reasoning. > >> >> -- >> Sandip >> >> --- >> www.funonrails.com > > 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. > >-- Sandip --- www.funonrails.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.
Sandip Ransing wrote:> Marnen thx for reply ! > > Actually I wanted to add "OR" condition to acts_as_mappable finders, > So that result-set will contain my own preferences too...but now it > looks like > for this purpose i may either need to mess up with library of > rails_geokit plugin > or override source of named_scope :(No. Don''t. It''s a bad idea. It should never be necessary. Did you read the link I posted? 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 17 August 2010 23:02, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sandip Ransing wrote: >> Marnen thx for reply ! >> >> Actually I wanted to add "OR" condition to acts_as_mappable finders, >> So that result-set will contain my own preferences too...but now it >> looks like >> for this purpose i may either need to mess up with library of >> rails_geokit plugin >> or override source of named_scope :( > > No. Don''t. It''s a bad idea. It should never be necessary. Did you > read the link I posted?Marnen, I am not sure what you are saying should never be necessary. Suppose I have named_scope_a and named_scope_b. Are you saying it should never be necessary to find the set of records that includes all those in _a and those in _b? If not that then what is the best way? To provide a new named scope for the OR condition does not seem very DRY. 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.
hmm. I gone through it .. but its necessary for me. Is there any workaround ? On Wed, Aug 18, 2010 at 3:32 AM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Sandip Ransing wrote: > > Marnen thx for reply ! > > > > Actually I wanted to add "OR" condition to acts_as_mappable finders, > > So that result-set will contain my own preferences too...but now it > > looks like > > for this purpose i may either need to mess up with library of > > rails_geokit plugin > > or override source of named_scope :( > > No. Don''t. It''s a bad idea. It should never be necessary. Did you > read the link I posted? > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Sandip --- www.funonrails.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 Tue, Aug 17, 2010 at 9:39 PM, Sandip Ransing <san2821-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is it possible to have two named scopes that can be joined using OR condition.How about result = (named_scope_1 + named_scope_2).uniq -- 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 18 August 2010 08:44, Franz Strebel <franz.strebel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Aug 17, 2010 at 9:39 PM, Sandip Ransing <san2821-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Is it possible to have two named scopes that can be joined using OR condition. > > How about > > result = (named_scope_1 + named_scope_2).uniqThat would loose the sort order I think, though one could always re-sort of course. Also could one use methods such as first on the result? Or apply another named scope to the result to further refine 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 18, 2010 at 9:57 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> That would loose the sort order I think, though one could always > re-sort of course. Also could one use methods such as first on the > result? Or apply another named scope to the result to further refine > it?Yes, the sort order gets lost. When you add two named_scopes, the result is an Array. So you can call first on it, but you can''t chain another named scope call to it. -- 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.
Franz, what you said i was doing this but it''s not good performance wise as it fires 2 queries also purpose of pagination will be for toss bcoz it will load objects and then do pagination. Colin, I am completely agree with your opinion and thinking there should be option for doing this. On Wed, Aug 18, 2010 at 1:27 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 August 2010 08:44, Franz Strebel <franz.strebel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Tue, Aug 17, 2010 at 9:39 PM, Sandip Ransing <san2821-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > >> Is it possible to have two named scopes that can be joined using OR > condition. > > > > How about > > > > result = (named_scope_1 + named_scope_2).uniq > > That would loose the sort order I think, though one could always > re-sort of course. Also could one use methods such as first on the > result? Or apply another named scope to the result to further refine > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Sandip --- www.funonrails.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 18 August 2010 09:15, Sandip Ransing <san2821-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: Please don''t top post, it makes it difficult to follow the thread.> Franz, > what you said i was doing this but it''s not good performance wise as it > fires 2 queries also > purpose of pagination will be for toss bcoz it will load objects and then do > pagination. > > Colin, > I am completely agree with your opinion and thinking there should be option > for doing this.Also please quote the bit of the email you are responding to. What opinion of mine is it that you agree with? I was not aware that I had expressed any opinion on your suggestion. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 17 August 2010 23:02, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> No. �Don''t. �It''s a bad idea. �It should never be necessary. �Did you >> read the link I posted? > > Marnen, I am not sure what you are saying should never be necessary. > Suppose I have named_scope_a and named_scope_b. Are you saying it > should never be necessary to find the set of records that includes all > those in _a and those in _b?No. I am saying that it should never be possible -- or necessary -- to do Person.scope_a.and_some_more_records . IMHO, that defeats the purpose of named_scopes, because the results of the scope chain are no longer guaranteed to be within scope_a. That''s dangerous. If this syntax were possible, then there would be no obvious difference between a proper chain like Person.red_hair.blue_eyes and an improper chain like Person.red_hair.oh_and_everyone_else_too.> If not that then what is the best way? > To provide a new named scope for the OR condition does not seem very > DRY.It''s a completely different scope from either of its constituents. What''s repetitive about that, particularly if you factor out duplication in other ways? I''d actually support a syntax for union queries such as Person.union_scope(:scope_a, :scope_b). This makes it obvious that it is not a simple scope chain, while still being simple to call.> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 August 2010 13:42, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote: >> On 17 August 2010 23:02, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> >> wrote: >>> No. �Don''t. �It''s a bad idea. �It should never be necessary. �Did you >>> read the link I posted? >> >> Marnen, I am not sure what you are saying should never be necessary. >> Suppose I have named_scope_a and named_scope_b. Are you saying it >> should never be necessary to find the set of records that includes all >> those in _a and those in _b? > > No. I am saying that it should never be possible -- or necessary -- to > do Person.scope_a.and_some_more_records . IMHO, that defeats the > purpose of named_scopes, because the results of the scope chain are no > longer guaranteed to be within scope_a. That''s dangerous. If this > syntax were possible, then there would be no obvious difference between > a proper chain like Person.red_hair.blue_eyes and an improper chain like > Person.red_hair.oh_and_everyone_else_too.Right, understood.> [...] > I''d actually support a syntax for union queries such as > Person.union_scope(:scope_a, :scope_b). This makes it obvious that it > is not a simple scope chain, while still being simple to call.Is that not what the OP was asking for in the original post to quote from there:> Is it possible to have two named scopes that can be joined using OR condition.To which you replied:> Goodness, I hope not. That would be a terrible misuse of named_scopes.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.
Marnen Laibow-Koser
2010-Aug-18 16:33 UTC
Re: Re: Re: Re: rails named scopes joined with OR
Colin Law wrote: [...]>> [...] >> I''d actually support a syntax for union queries such as >> Person.union_scope(:scope_a, :scope_b). This makes it obvious that it >> is not a simple scope chain, while still being simple to call. > > Is that not what the OP was asking for in the original post to quote > from there: > >> Is it possible to have two named scopes that can be joined using OR condition. > > To which you replied: > >> Goodness, I hope not. That would be a terrible misuse of named_scopes.I had thought that the OP was specifically asking about named_scope chaining with OR as in the other thread. On rereading, however, it seems that you''re right and I misunderstood the question.> > ColinBest, -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.