0

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"."...

dagda1
  • 26,856
  • 59
  • 237
  • 450

1 Answers1

1

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

Jeremy Evans
  • 11,959
  • 27
  • 26