I've been trying with no success to translate the following SQL query into ActiveRecord one:
SELECT invited.id
FROM (
SELECT users.id
FROM users
WHERE users.invited_by_id IS NOT NULL
) AS invited
JOIN (
SELECT id
FROM users
WHERE users.id NOT IN (
SELECT user_id
FROM report_logs
)
) AS no_report ON invited.id = no_report.id;
I've been using the following command but it doesn't return the same results as the SQL version.
@users = User.includes(:report_logs)
.on(report_logs: { user_id: nil })
.where("invited_by_id IS NOT NULL")
The requirement for the query is to Find all the users who have been invited by another user and do not possess a report linked to its id.