我遇到的问题与 RubyOnRails Guide 中暴露的问题非常相似.
此查询:
Client.all :joins => :orders,
:conditions => { :orders => {:created_at => time_range}}
如果客户在时间范围内下了一些订单,则应返回所有客户的订单。我说得对吗?
我想要的略有不同:我想要所有的客户,无论他们是否在时间范围内下订单。如果他们下了一些订单,我也想要。我想出了这个:
Client.all :joins => 'LEFT OUTER JOIN orders ON orders.client_id = clients.id',
:conditions => {:orders => {:created_at => time_range}})
但这并没有给我提供未下订单的客户...有人可以帮忙吗?
最佳答案
我认为这可以做到:
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]
关于mysql - ActiveRecord MySQL 和联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/840794/