title: Rails 4 eliminating entries in ActiveRecord::Relation that are similar to those contained in another
body: <p>I have a selection box in a view that allows a user to pick a user to share something with.</p>
  
  <pre><code>&lt;%=
    f.collection_select :user_id, 
      @users, 
      :id, 
      :email, 
      {prompt: 'Select a User to Share with'}, 
      class: 'form-control'
  %&gt;
  </code></pre>
  
  <p>I don't want the already picked people to show in the list.</p>
  
  <p>I am able to isolate the users in ActiveRecord::Relation objects, but I cannot figure out how to isolate just the ones that need to show up in the selection:</p>
  
  <p>None of these Relations come straight off of tables.</p>
  
  <p>If my existing Relations are "all_users" and "already_shared_with_users" how do I do the equivalent of:</p>
  
  <pre><code>select
    allu.user_id
  from
    all_users allu
    left outer join already_shared_with_users alsu
      on alsu.user_id = allu.user_id
  group by
    allu.user_id
    , alsu.user_id
  having
    alsu.user_id is null
  </code></pre>
  
  <p>basically returning a Relation of the NOT already-shared-with users?</p>
  
  <p>Here's what I've tried:</p>
  
  <p>I've tried joins, but they don't work because I'm not using tables, nor associations:</p>
  
  <pre><code>potential_sharing_users =
    all_users
    .joins("already_shared_with_users")
      .where("already_shared_with_users" =&gt; { id: nil })
  </code></pre>
  
  <p>User class scopes won't suffice because the already_shared_with_users relation requires knowing the parameter id value of the thing I'm sharing (not the user) passed from the index view, and I'm not on the user view anyhow.</p>
  
  <p>The one thing that I've held off on is going straight to SQL, especially since I don't know how yet and I wanted to try it the Rails way.</p>
  
  <p>Speaking of, I found the following which will cover that part, I just need to pass in the aforementioned parameter:
  <a href="https://stackoverflow.com/questions/24345959/rails-activerecord-relation-using-subquery-as-a-table-for-a-sql-select-statemen">Rails Activerecord Relation: using subquery as a table for a SQL select statement</a>
  and
  <a href="http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-from" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-from</a></p>
  
  <p>I'm new to Rails and Ruby, so I there's something obvious I'm missing here. Perhaps I shouldn't with Relations and collection_select since I'm not trying to delete anything from a table, just display it in a limited fashion. I did find <a href="http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease" rel="nofollow noreferrer">http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease</a> which should shed some light on alternatives.</p>
  
  <p>Thanks.</p>