活动具有属性:start_date,:end_date
发票具有属性:start_date,:end_date
竞选活动
has_many:invoices
发票
belongs_to:campaign
我想得到那些
start_date
小于当前日期AND
且end_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/