diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index fa3c28f7d4731..ba6a0ab89be3b 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -652,7 +652,7 @@ def construct_finder_sql_with_included_associations(reflections) def extract_association_for_record(record, rows, reflection) association = rows.collect do |row| - if row["#{table_name}__#{primary_key}"] == record.id.to_s + if row["#{table_name}__#{primary_key}"] == record.id.to_s && !row["#{reflection.klass.table_name}__#{reflection.klass.primary_key}"].nil? reflection.klass.send(:instantiate, extract_record(reflection.klass.table_name, row)) end end