Gabriele Tassoni
2009-Feb-04 23:06 UTC
Concatenate (+) Named scopes leads to creation of an Array, is there a way to have stipp a Scope?
Hi, I try to explain better, I wrote a named scope like that: named_scope :with_cellphones_or_emails, {:include => [:cellphones, :emails], :conditions => ["(cellphones.active = ? AND cellphones.customer_id = customers.id) OR (emails.active = ? AND emails.customer_id = customers.id)", true, true]} for the Customer model, calling Customer.with_cellphones_or_emails takes about 6 seconds on my macbook unibody with 2GB Ram, I''ve made some tests, I wrote two more named_scopes: named_scope :with_emails, {:joins => :emails, :conditions => {"emails.active" => true}} named_scope :with_cellphones, {:joins => :cellphones, :conditions => {"cellphones.active" => true}} running Customer.with_cellphones takes approx. 50ms, Customer.with_emails takes approx 5ms (yeah, my customers have more cellphones than emails! ^_^)... 55ms vs 6s! °_° (these are mysql calculated times)... Trying to minimize the time spent on db query, by single query, I thought it could be great if I can concatenate the two results, indeed I can do: Customer.with_cellphones + Customer.with_emails but the resulting object is an Array, is there a way to obtain an ActiveRecord::NamedScope::Scope object? Thank you for the answer. G. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-05 09:47 UTC
Re: Concatenate (+) Named scopes leads to creation of an Array, is there a way to have stipp a Scope?
On Feb 4, 11:06 pm, Gabriele Tassoni <gabriele.tass...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Trying to minimize the time spent on db query, by single query, I > thought it could be great if I can concatenate the two results, indeed > I can do: > > Customer.with_cellphones + Customer.with_emails > > but the resulting object is an Array, is there a way to obtain an > ActiveRecord::NamedScope::Scope object? >Not that I can think of. even if it did work I expect you;d just end up with a scope that it is equivalent to the first, slow, scope since a scope is by definition not much more than a set of options to pass to a single call to find (or count etc...) Fred> Thank you for the answer. > > G.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Gabriele Tassoni
2009-Feb-05 12:39 UTC
Re: Concatenate (+) Named scopes leads to creation of an Array, is there a way to have stipp a Scope?
On 5 Feb, 10:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 4, 11:06 pm, Gabriele Tassoni <gabriele.tass...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote:Hi Frederick, thank you for the anwser.> > Not that I can think of. even if it did work I expect you;d just end > up with a scope that it is equivalent to the first, slow, scope since > a scope is by definition not much more than a set of options to pass > to a single call to find (or count etc...) >I can understand, but a find returns an Array, a named_scope, a Scope... It looks like there''s a bit of inconsistency on how active record results are showed.. I would have expected that any, ehrm.. say... list of results would be always the same object, and that there would be a way to upgrade a resultset expressed as an Array to a Scope (that''s, from what i can see, the same array proxied as a Scope) or any other resultset format used.. G. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-05 14:13 UTC
Re: Concatenate (+) Named scopes leads to creation of an Array, is there a way to have stipp a Scope?
On 5 Feb 2009, at 12:39, Gabriele Tassoni wrote:> > > > On 5 Feb, 10:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On Feb 4, 11:06 pm, Gabriele Tassoni <gabriele.tass...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: > > Hi Frederick, > thank you for the anwser. >> >> Not that I can think of. even if it did work I expect you;d just end >> up with a scope that it is equivalent to the first, slow, scope since >> a scope is by definition not much more than a set of options to pass >> to a single call to find (or count etc...) >> > I can understand, but a find returns an Array, a named_scope, a > Scope... It looks like there''s a bit of inconsistency on how active > record results are showed.. I would have expected that any, ehrm.. > say... list of results would be always the same object, and that there > would be a way to upgrade a resultset expressed as an Array to a Scope > (that''s, from what i can see, the same array proxied as a Scope) or > any other resultset format used..a Scope isn''t a result set really, just some options for find. (associations are a bit different, but that''s a whole other kettle of fish) 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---