dagda1
dagda1

Reputation: 28890

SEQUEL - Subquery count syntax

Can anyone help me with the sequel syntax for the following the ruby orm sequel:

SELECT *, (SELECT COUNT(*) FROM todos WHERE reference_email_id = "emails".id) todo_count 
FROM "emails" 
INNER JOIN "email_participants" 
ON ("email_participants"."email_id" = "emails"."id") 
WHERE ("user_id" = 1)

I cannot quite get the syntax, I have this so far:

scope = Email.inner_join(:email_participants, {email_id: :id})
                  .where(user_id: query.user_id)
                  .select_append {
                    Attachment.where(reference_email_id: Sequel.qualify(:emails, :id))
                      .count(:id)
                      .exists
                      .as(:attachment_count)
                  }

I get the following error:

missing FROM-clause entry for table "emails" LINE 1: ... FROM "attachments" WHERE ("reference_email_id" = "emails"."...

Upvotes: 0

Views: 864

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12149

My guess is you should remove the .exists line. It's hard to say conclusively since you didn't post the SQL produced.

Upvotes: 1

Related Questions