如何使用Mailboxer在rails中获取未读邮件收据的用户

时间:2015-05-04 20:17:00

标签: ruby-on-rails devise mailboxer

我试图根据他们的" is_read"获取所有未读的用户消息列表。邮箱收据。我一直试图用以下几行来做到这一点:

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

这是我得到的错误:

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)>'

我可以毫无问题地调用User.joins(:收据),但只要我尝试获取is_read状态,就会发生任何事情。我已阅读this thread并且似​​乎使用了推荐的格式,但仍然无法使其正常运行。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

将其更改为

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

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