Concatenate (+) Named scopes leads to creation of an Array, is there a way to have stipp a Scope?


#1

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.


#2

On Feb 4, 11:06 pm, Gabriele T. removed_email_address@domain.invalid
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


#3

On 5 Feb, 10:47, Frederick C. removed_email_address@domain.invalid wrote:

On Feb 4, 11:06 pm, Gabriele T. removed_email_address@domain.invalid
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.


#4

On 5 Feb 2009, at 12:39, Gabriele T. wrote:

Not that I can think of. even if it did work I expect you;d just end
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