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

**URL:** <https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485>\
**Category:** Rails\
**Created:** [February 5, 2009, 12:07am UTC](https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485 "2009-02-05T00:07:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [February 5, 2009, 12:07am UTC](https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485/1 "2009-02-05T00:07:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [February 5, 2009, 10:48am UTC](https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485/2 "2009-02-05T10:48:23Z")

</div>

On Feb 4, 11:06 pm, Gabriele T. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [February 5, 2009, 1:40pm UTC](https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485/3 "2009-02-05T13:40:03Z")

</div>

On 5 Feb, 10:47, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 4, 11:06 pm, Gabriele T. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [February 5, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/concatenate-named-scopes-leads-to-creation-of-an-array-is-there-a-way-to-have-stipp-a-scope/159485/4 "2009-02-05T15:14:21Z")

</div>

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
