Active Record has_many generates sql with foreign key IS NULL <p>I don't expect a model with NULL as foreign key to belong to anything!</p> <p>I have the following rails app, modelling ants and ant hills (inspired by Jozef).</p> <pre><code>$ rails -v Rails 3.2.8 $ rails new ant_hill $ cd ant_hill </code></pre> <p>Create the ant hill and ant models. An ant can belong to an ant hill and an ant hill can have many ants.</p> <pre><code>$ rails generate model AntHill name:string $ rails generate model Ant name:string ant_hill_id:integer $ vim app/models/ant.rb $ cat app/models/ant.rb class Ant &lt; ActiveRecord::Base belongs_to :ant_hill end $ vim app/models/ant_hill.rb $ cat app/models/ant_hill.rb class AntHill &lt; ActiveRecord::Base has_many :ants end $ rake db:migrate == CreateAntHills: migrating ================================================= -- create_table(:ant_hills) -&gt; 0.0013s == CreateAntHills: migrated (0.0016s) ======================================== == CreateAnts: migrating ===================================================== -- create_table(:ants) -&gt; 0.0035s == CreateAnts: migrated (0.0037s) ============================================ </code></pre> <p>Run the following code in a console.</p> <pre><code>$ rails c Loading development environment (Rails 3.2.8) </code></pre> <p>Create a couple of ants, persisted, that don't belong to any ant hill.</p> <pre><code>1.9.2-p290 :001 &gt; Ant.create! name: "August" =&gt; #&lt;Ant id: 1, name: "August", ant_hill_id: nil, created_at: "2012-09-27 12:01:06", updated_at: "2012-09-27 12:01:06"&gt; 1.9.2-p290 :002 &gt; Ant.create! name: "Bertil" =&gt; #&lt;Ant id: 2, name: "Bertil", ant_hill_id: nil, created_at: "2012-09-27 12:01:13", updated_at: "2012-09-27 12:01:13"&gt; </code></pre> <p>Now instantiate an ant hill, but don't save it just yet.</p> <pre><code>1.9.2-p290 :003 &gt; ant_hill = AntHill.new name: "Storkullen" =&gt; #&lt;AntHill id: nil, name: "Storkullen", created_at: nil, updated_at: nil&gt; </code></pre> <p>I expect this ant hill to not have any ants and it doesn't.</p> <pre><code>1.9.2-p290 :004 &gt; ant_hill.ants =&gt; [] </code></pre> <p>I still expect the ant hill to not have any ants but now it has two.</p> <pre><code>1.9.2-p290 :005 &gt; ant_hill.ants.count (0.1ms) SELECT COUNT(*) FROM "ants" WHERE "ants"."ant_hill_id" IS NULL =&gt; 2 </code></pre> <p>Same here, it should never generate a query containing "IS NULL" when dealing with foreign keys. I mean "belongs_to NULL" can't belong to anything, right?</p> <pre><code>1.9.2-p290 :006 &gt; ant_hill.ants.all Ant Load (0.4ms) SELECT "ants".* FROM "ants" WHERE "ants"."ant_hill_id" IS NULL =&gt; [#&lt;Ant id: 1, name: "August", ant_hill_id: nil, created_at: "2012-09-27 12:01:06", updated_at: "2012-09-27 12:01:06"&gt;, #&lt;Ant id: 2, name: "Bertil", ant_hill_id: nil, created_at: "2012-09-27 12:01:13", updated_at: "2012-09-27 12:01:13"&gt;] </code></pre> <p>After it is persisted it behaves as expected.</p> <pre><code>1.9.2-p290 :007 &gt; ant_hill.save! =&gt; true 1.9.2-p290 :008 &gt; ant_hill.ants.count (0.4ms) SELECT COUNT(*) FROM "ants" WHERE "ants"."ant_hill_id" = 1 =&gt; 0 1.9.2-p290 :009 &gt; ant_hill.ants.all Ant Load (0.4ms) SELECT "ants".* FROM "ants" WHERE "ants"."ant_hill_id" = 1 =&gt; [] </code></pre> <p>Any insight? Is this the expected behavior?</p>