title: Rails filtering child collections to restrict what data a user can view
body: <p>I'm using rails 4.2.0 with Postgres 9.4.4. Let's say I have a model <code>Test</code> that has a has many association called <code>completions</code> and <code>completions</code> has a <code>user_id</code> field. Say, on the front end I want to display all the Tests available to a user and I want to grey out the Tests the user has already completed. I want to only make one request to the api, getting a JSON array of all the tests available to the user which would include a <code>completions</code> array instead each object, which would only show completion objects that have a user_id belonging to the user viewing the list. 
  So say user with <code>id: 1</code>(user1) logs in and goes to the tests index view, the format of the response I want is:</p>
  
  <pre><code>[{"id": 1, "name": "sample_test1", "completions":[{"user_id": 1,.... }]}.....]
  </code></pre>
  
  <p>So essentially if there was another user with <code>id:2</code>(user2) that also took <code>sample_test1</code>, when user1 logs in, they would not see an object inside the completions array that has an id of anything other than 1. Similarly, if user2 was logged in, they would not be able to see any object in the <code>completions</code> array that has an id of anything other than <code>2</code>. What would be the easiest way to construct a query to get the data in the above format?</p>