Home > Net >  rails get record that is not exist on right join
rails get record that is not exist on right join

Time:01-25

I am trying to get a query for a join table, where the record in right table is nonexistent, for a specific param.

Let say that a user should post email every day, and the email model will be created with a date. I want to get users who did not post an email on a specific day.

I tried using not exist and also not in but to no avail. Or maybe my syntax is incorrect or I am missing something.

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

Is this possible?

Thanks

CodePudding user response:

You could try from different angle:

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