Lorenz
Lorenz

Reputation: 775

How to get users with unread message receipts with Mailboxer in rails

I'm trying to get a list of all User messages that are unread based on their "is_read" mailboxer receipt. I've been trying to do this with the following line:

user_ids = User.
                joins(:receipts).
                where(receipts: {is_read: false}).
                select('DISTINCT users.id').
                map(&:id)

This is the error I get:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "receipts"
LINE 1: ...D "mailboxer_receipts"."receiver_type" = $1 WHERE "receipts"...
                                                             ^
: SELECT DISTINCT users.id FROM "users" INNER JOIN "mailboxer_receipts" ON "mailboxer_receipts"."receiver_id" = "users"."id" AND "mailboxer_receipts"."receiver_type" = $1 WHERE "receipts"."is_read" = 'f'
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/schedule_send_notifications_job.rb:10:in `perform'
/Users/lorenzsell/DEV/Heartbeat-pods/lib/tasks/email_tasks.rake:5:in `block (2 levels) in <top (required)>'
-e:1:in `<main>'
PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "receipts"
LINE 1: ...D "mailboxer_receipts"."receiver_type" = $1 WHERE "receipts"...
                                                             ^
/Users/lorenzsell/DEV/Heartbeat-pods/app/jobs/schedule_send_notifications_job.rb:10:in `perform'
/Users/lorenzsell/DEV/Heartbeat-pods/lib/tasks/email_tasks.rake:5:in `block (2 levels) in <top (required)>'

I can call User.joins(:receipts) with no problems, but as soon as I try to get the is_read status, everything borks. I've read this thread and seem to be using the recommended format, but can't still can't get this to work. Am I missing something?

Upvotes: 0

Views: 289

Answers (1)

usha
usha

Reputation: 29349

Change it to

user_ids = User.
                joins(:receipts).
                where("mailboxer_receipts.is_read" => false}).
                select('DISTINCT users.id').
                map(&:id)

or

user_ids = User.
                joins(:receipts).
                where(:mailboxer_receipts => {:is_read => false}).
                select('DISTINCT users.id').
                map(&:id)

Upvotes: 1

Related Questions