ActiveRecord query based on status of associated records
<p>I'm having trouble to build this query in ActiveRecord:</p>

<p>Return a list of unique documents that have no completed or active print jobs.</p>

<p>So the query should include documents that have either no print jobs at all or only aborted print jobs.</p>

<p>Here are the models and relevant attributes:</p>

<pre><code>class Document &lt; ActiveRecord::Base
  has_many :print_jobs
end

class PrintJob &lt; ActiveRecord::Base
  enum status: { active: 0, completed: 1, aborted: 2 }

  belongs_to :document
end
</code></pre>

<p>My first attempt was to get a list of all completed or active print jobs, and find all documents that are not associated with these print jobs:</p>

<pre><code>Document.where.not(id: PrintJob.active_or_completed.select(:document_id))
</code></pre>

<p>But I wonder if this is the right "SQL-ish" way of doing such queries.</p>

<p>Thanks in advance for your help!</p>