Maybe this is being silly, but couldn't you just add a flag to your  
customer model?
Something like 'is_contactable', that would be updated on save? A  
little denormalization
to get a big performance increase isn't that bad...

Other ideas:
- make both cellphones and emails subclasses of a single object?
- I haven't played with it enough, but maybe some kind of inner join?  
After all, you're
only interested in records with at least one associated cellphone or  
email.

Finally, have you tried using the MySQL query debugging stuff (EXPLAIN  
and friends)?

--Matt Jones

On Feb 4, 2009, at 7:01 PM, Gabriele Tassoni wrote:

>
> From what I can see, chaining them is not like saying "either ...
> or ..." i.e. I have 2600 customers with cellphones, and 4 with emails,
> with 3 that have both, What I want is obtain 2601 records so far (it
> would be good even to have 2604, I can add a :group condition...), but
> what I obtain is 3 records, the customers that have both an email and
> a cellphone... so, chaining is like intersection, not concatenation...
>
> On 5 Feb, 00:50, kevinpfromnm <[email protected]> wrote:
>> You can't just chain your named_scopes?
>> Customer.with_cellphones.with_emails
>>
>> On Feb 4, 4:11 pm, Gabriele Tassoni <[email protected]>
>> wrote:
>>
>>> 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 it:
>>
>>> @customers = Customer.with_cellphones + Customer.with_emails
>>
>>> but the resulting object is an Array, the index action of the
>>> controller is like this one:
>>
>>> hobo_index @customers, :order => :name, :group => "customers.id"
>>
>>> is there a way to obtain an ActiveRecord::NamedScope::Scope object
>>> from that concatenation or to let hobo_index accept the Array?
>>
>>> Thank you for the answer.
>>
>>> G.
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to