title: relation one_to_one: get a list of non-linked "belongs_to" entities
body: <p>I have an optimization question. I search how to use ActiveRecord to do a request to get all entities in a one_to_one or one_to_many which don't have any link.</p>
  
  <p>I have:</p>
  
  <pre><code>class Model1 &lt; ActiveRecord::Base
    has_one :model2
    ...
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Model2 &lt; ActiveRecord::Base
    belongs_to :model1
    ...
  </code></pre>
  
  <p>If I want the list of all model2s non-link, I just have to do:</p>
  
  <pre><code>unlinked_model2s = Model2.where(:model1_id =&gt; nil)
  </code></pre>
  
  <p>But how I do the same for the model1s? I would have the list of all model1s which are not linked to a model2.</p>
  
  <p>I tried many things, but the only way to make it works, is to to all the requests one by one, which is horrible:</p>
  
  <pre><code>unlinked_model1s = Array.new
  Model1.all.each do |model1|
    unless model1.model2
      unlinked_model1s &lt;&lt; model1
    end
  end
  </code></pre>
  
  <p>Thank you for your help!</p>