title: Rails has_many through query
body: <p>I have many-to-many relationship with User, Attachment and Form model.</p>
  
  <p>I want to reach to attachments not belongs to a user. I will try something like this but did not work.</p>
  
  <pre><code>Attachment.includes(:forms,:users).where.not('forms.user_id  = ?', @user.id).references(:forms,:users)
  </code></pre>
  
  <p>I have tried more but did't find the correct one.</p>
  
  <blockquote>
    <p>user.rb</p>
  </blockquote>
  
  <pre><code>  has_many :forms
    has_many :attachments, through: :forms
  </code></pre>
  
  <blockquote>
    <p>attachment.rb</p>
  </blockquote>
  
  <pre><code>  has_many :forms
    has_many :users, through: :forms
  </code></pre>
  
  <blockquote>
    <p>forms.rb</p>
  </blockquote>
  
  <pre><code>  belongs_to :user
    belongs_to :attachment
  </code></pre>
  
  <blockquote>
    <p>Update:</p>
  </blockquote>
  
  <p>I m still finding an answer</p>
  
  <pre><code>Attachment.includes(:forms).where(forms: {user_id: user.id}).references(:forms)
  </code></pre>
  
  <p>is working but where.not returns empty</p>
  
  <p>I think where.not only looks the attachments related to forms not all of them</p>