views:

136

answers:

1

Hey,

I have a problem very similar to the one exposed here in RubyOnRails Guide.

This query :

Client.all :joins => :orders, 
           :conditions => { :orders => {:created_at => time_range}}

should return all the clients with their orders if they made some orders within the time range. Am I right?

What I want is slightly different : I want all the client wether they made an order or not in the time range. If they made some orders, I want them though. I came up with that :

Client.all :joins => 'LEFT OUTER JOIN orders ON orders.client_id = clients.id',
           :conditions => {:orders => {:created_at => time_range}})

But this doesn't give me the clients who didn't make an order... Can someone help with that?

+1  A: 

I think this will do it:

Client.all :joins => 'LEFT OUTER JOIN orders ON orders.client_id = clients.id',
       :conditions => ['orders.created_at IN (?) OR orders.id IS NULL', time_range]
sprsquish
But isn't that cheating?... let me try it.
Julien Genestoux
That worked ;) Thx...
Julien Genestoux
I don't know how to do OR conditions in AR without a plugin or writing the query by hand. Maybe I've missed something along the way.
sprsquish