Coda Hale
2006-Jul-31 04:50 UTC
Patch for #3438: Eager loading doesn''t respect :order of associations
Hello all, I still have a patch for #3438 (eager loading doesn''t respect association orders); it passes all unit tests, and has additional unit tests with full coverage. The patch is attached. == The Bug = Author.find(1).posts != Author.find(1, :include => [:posts]).posts if Author has_many :posts, :order => anything. This means that either one must avoid eager loading or constantly specify an :order option (despite having already specified one in the association options). Awkward. http://dev.rubyonrails.org/ticket/3438 == The Solution = My patch adds ~20 lines of code to ActiveRecord::Associations::ClassMethods#construct_finder_sql_with_included_associations which run through the various associations in the join dependency, extract all :order options, expand them to their full form (e.g., ''posts.published_at'' instead of ''published_at'', minding table aliases), and append them to the SQL''s ORDER BY clause, if any exists (creating one if not). == The Catch = Developers who have previously relied on their database''s default order (if any exists) may be unpleasantly surprised to find that their records are not coming back in the expected order. For example: Author.find(:all) != Author.find(:all, :include => [:posts]) It has been suggested by some that these developers would do well to be surprised, as not specifying an ORDER BY clause means that the order of the records is not meaningful to you, and that relying upon an implementation quirk is bad form. This "fire hot" school of pedagogy is certainly effective, but may result in additional support demands. == The Good News = This patch passes all existing unit tests, and adds two additional tests which assert the proper behavior over a wide variety of model types, including STI, Polymorphic, Rich Joins, etc., and even cascaded eager loading. 2 tests, 149 assertions. == The Previously Addressed Issues = My original version of this patch assumed a default SQL sort order. It no longer does so. == The Begging = C''mon. Please. Dude. C''mon. Duuuude. Please. C''mon. Please? Awww.... dude. Dude? C''mon? == The End = Since Trac is on sabbatical, I figured I''d toss up my patch here, in the hopes that it will be applied. The last time I tried this I got a lot of great feedback and even some publicity--thanks, Rodney!--but everyone was at RailsConf, and I guess that was more fun than fixing my pet peeves. ;-) Thanks! -- Coda Hale http://blog.codahale.com _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Maybe Matching Threads
- [PATCH] (+tests) with_scope :order doesn''t work with included associations
- how to ? Rails 3 ActiveRecord eager loading and AREL
- Typo 1055 created_at Vs. published_at
- [PATCH] Access to more Arel predicate types from where condition hash
- ActiveRecord with different Date/Time format