rails 获取右连接中不存在的记录

rails get record that is not exist on right join

我正在尝试获取连接 table 的查询,其中右侧 table 的记录对于特定参数不存在。

假设用户应该 post 每天发送电子邮件,并且将创建带有日期的电子邮件模型。我想获得在特定日期没有 post 电子邮件的用户。

我尝试使用 not exist and also not in 但无济于事。或者我的语法不正确或者我遗漏了什么。

User.joins(:emails).where("emails.date not in ('2021-01-24') and emails.id is null)

这可能吗?

谢谢

你可以换个角度试试:

date = Date.parse('2021-01-24')
user_ids = Email.where(date: date).pluck(:user_id)
User.where.not(id: user_ids)