rails/rails — pull request #10901 by armstrjare (2013-06-10 04:13:10+00:00)
Title: Prevent query of null foreign key value on new record assoc collection ids_reader

`Associations::CollectionAssociation#ids_reader` triggered for a non-persisted owning record would incorrectly trigger a query for records with a `NULL` foreign key value. This would result in querying and returning IDs of all non-associated records, rather than an empty set as would be expected.

For example:

```
Company.new.contract_ids
"SELECT `contracts`.id FROM `contracts` WHERE `contracts`.`company_id` IS NULL"
```

Besides the incorrect return value, on large tables this could potentially have a material performance impact.

With this change, the above query won't be executed and the `ids_reader` will simply map over the cached association values (which would be an empty set for new records, unless any had been added).