活动具有属性:start_date,:end_date
发票具有属性:start_date,:end_date
竞选活动

has_many:invoices

发票
belongs_to:campaign

我想得到那些start_date小于当前日期ANDend_date介于campaign.invoices.last.created..current_date之间的cmpaigns
我怎么查询这个?

最佳答案

试试这个:

 Compaign.joins(:invoices)
            .where("compaigns.start_date < :today AND compaigns.end_date < :today", today: Date.today)
            .group("compaigns.id")
            .having("compaigns.end_date > MAX(invoices.created_at)")

关于mysql - 在Rails中查询联接关联,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35309074/

10-10 17:03