获取没有关联数据的所有行

时间:2016-02-11 06:35:44

标签: mysql ruby-on-rails activerecord include

广告系列包含属性:start_date,:end_date

发票包含属性:start_date,:end_date

campaign.rb

has_many:invoices

invoice.rb

belongs_to:campaign'

我想让所有没有发票并且end_date小于当前日期的广告系列。

我试过这个

Campaign.includes(:invoices).where("compaigns.end_date > ? ",Date.today, :invoices => { :campaign_id => nil } ).count

我是怎么做到的?

1 个答案:

答案 0 :(得分:2)

我想你差不多了。给出的示例不起作用,因为它混合了where的字符串和散列形式。尝试将条件分为两个where调用:

Campaign.includes(:invoices).
  where("campaigns.end_date > ? ",Date.today).
  where(invoices: {campaign_id: nil}).count