Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11427 (closed defect: fixed)

Opened 6 months ago

Last modified 6 months ago

[PATCH] Don't treat NilClass like ActiveRecord::Base - housekeeping patch for preloaded associations

Reported by: danger Assigned to: core
Priority: high Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: major Keywords:
Cc:

Description

In the association preload code there's this pattern showing up in a couple places:

through_records.first.class.preload_associations(through_records, source)
through_records.compact.each { |through_record| ... }
# works when through_records is [#<User>, #<User>] but fails BAD when through_records is [nil, #<User>]

The through_records array should be compacted before any of this code.

This patch compacts the two main arrays in this module (records and through_records) just once and at the right time. I could contrive a regression test for this but it's an easy fix and shouldn't need to be addressed again. If anyone wants me to add a test just say so.

Existing tests all pass.

Attachments

properly_compacting_records_arrays_to_avoid_nil_errors.diff (2.4 kB) - added by danger on 03/26/08 00:21:59.

Change History

03/26/08 00:21:59 changed by danger

  • attachment properly_compacting_records_arrays_to_avoid_nil_errors.diff added.

03/26/08 15:27:03 changed by rick

  • status changed from new to closed.
  • resolution set to fixed.

(In [9094]) More efficient association preloading code that compacts a through_records array in a central location. Closes #11427 [danger]