Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

eager loading inconsistency

#64

Eager loading a has_one with :order is not consistent.

Post.has_many :comments

Post.has_one :latest_comment, :class_name => 'Comment', :order => 'created_at desc'

(let's assume that comment ids increase in step with created_at)

Post.find(:all).latest_comment.id #=> 5

Post.find(:all, :include => :latest_comment).latest_comment.id #=> 4

Post.find(:all, :include => :latest_comment, :conditions => 'comments.id is not null').latest_comment.id #=> all bets are off - fallback to old eager loading ignores latest_comment :order

The inconsistency for the non-fallback eager loading stems from the fact that the set_xxx_target method is called for each relevant result, ultimately favoring the last record in the set.

The non-eager load will :limit to 1 record, favoring the first record in the set.

Sadly, the strategy of making new-style eager loading the default (rather than an explicit option to find()) and having an automatic fallback to the old-style eager loading leaves no clear-cut winner here.

That said, when comparing fallback-eager's "all bets are off" to new-eager's "the exact opposite of what you asked for", I'd say new-eager makes the least sense.

The preloading call to set_xxx_target for has_one associations should use the first relevant record and discard the rest.

Reported by Trevor Squires · April 29th, 2008 @ 08:34 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Trevor Squires
    Trevor Squires

    Darn...

    typos in there, meant to say Post.find(:first) rather than Post.find(:all) everywhere.

    April 29th, 2008 @ 08:39 PM

  2. Frederick Cheung
  3. Michael Koziarski
    Michael Koziarski
    • State changed from new to resolved

    May 6th, 2008 @ 10:38 AM