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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-05 00:07
(Received via mailing list)
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-05 10:48
(Received via mailing list)
On Feb 4, 11:06 pm, Gabriele Tassoni <gabriele.tass...@gmail.com>
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
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-05 13:40
(Received via mailing list)
On 5 Feb, 10:47, Frederick Cheung <frederick.che...@gmail.com> wrote:
> On Feb 4, 11:06 pm, Gabriele Tassoni <gabriele.tass...@gmail.com>
> 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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-05 15:14
(Received via mailing list)
On 5 Feb 2009, at 12:39, Gabriele Tassoni 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
This topic is locked and can not be replied to.