This project is archived and is in readonly mode.
eager loading inconsistency
Reported by Trevor Squires | April 29th, 2008 @ 08:34 PM
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.
Comments and changes to this ticket
-
Trevor Squires April 29th, 2008 @ 08:39 PM
Darn...
typos in there, meant to say Post.find(:first) rather than Post.find(:all) everywhere.
-
Michael Koziarski May 6th, 2008 @ 10:38 AM
- State changed from new to resolved
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>