title: Find and destroy dependent records
body: <p>Over a period of time my Rails app has had various rewrites, and in some cases incorrect model associations.</p>
  
  <p>Currently my <code>User</code> model <code>has_many :posts</code> and its <code>destroy</code> method correctly removes all dependent <code>Post</code>s, but at times when things were improperly written this did not happen. I'm now left with a handful of Post records that cause errors all over because their User does not exist.</p>
  
  <p>What would be the most efficient way to manually filter through all Post records, check if its User actually exists, and if not destroy that Post?</p>
  
  <p>I'm imagining something like:</p>
  
  <pre><code>Post.all.select{ |post| post.user.nil? }.destroy
  </code></pre>
  
  <p>But that seems incredibly inefficient for thousands of records. I'd love to know the best way to do this. Thank you!</p>