Ticket #3438: eager_loading_order_patch_with_unit_tests.diff
| File eager_loading_order_patch_with_unit_tests.diff, 7.4 kB (added by coda.hale@gmail.com, 2 years ago) |
|---|
-
activerecord/test/associations_cascaded_eager_loading_test.rb
old new 103 103 authors.first.posts.first.special_comments.first.post.very_special_comment 104 104 end 105 105 end 106 107 def test_association_orders_work_with_cascaded_eager_loading 108 [ { :posts => :comments }, { :posts => :categories }, 109 { :posts => [:comments, :categories] }, 110 { :comments => :post }, { :funky_comments => :post } ].each do |association| 111 expected_order = Author.find(:all).map(&association.keys.first).flatten.map(&:id) 112 eagerly_loaded_order = Author.find(:all, :include => [association]).map(&association.keys.first).flatten.map(&:id) 113 assert_equal expected_order, eagerly_loaded_order, "Author.#{association.keys.first} has a different order when eagerly loaded via #{association.inspect}." 114 end 115 end 106 116 end -
activerecord/test/associations_go_eager_test.rb
old new 356 356 assert_equal 2, one.comments.size 357 357 assert_equal 2, one.categories.size 358 358 end 359 360 def test_association_orders_work_with_eager_loading 361 { Author => [ :posts, :posts_with_comments, :posts_with_categories, :posts_with_comments_and_categories, 362 :comments, :funky_comments, :special_posts, :hello_posts, :nonexistent_posts, 363 :posts_with_callbacks, :posts_with_proc_callbacks, :posts_with_multiple_callbacks, 364 :unchangable_posts, :author_favorites ], 365 Category => [ :posts, :special_posts, :hello_posts, :nonexistent_posts ], 366 DependentFirm => [ :companies ], 367 Firm => [ :clients, :clients_sorted_desc, :clients_of_firm, :dependent_clients_of_firm, 368 :exclusively_dependent_clients_of_firm, :plain_clients ], 369 Person => [ :readers, :posts ], 370 Post => [ :comments, :special_comments, :categories, :special_categories, :readers, :people ] 371 }.each do |model, associations| 372 associations.each do |association| 373 expected_order = model.find(:all).map(&association).flatten.map(&:id) 374 eagerly_loaded_order = model.find(:all, :include => [association]).map(&association).flatten.map(&:id) 375 assert_equal expected_order, eagerly_loaded_order, "#{model}.#{association} has a different order when eagerly loaded." 376 end 377 end 378 end 379 359 380 end -
activerecord/test/fixtures/post.rb
old new 15 15 16 16 has_one :very_special_comment 17 17 has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post 18 has_many :special_comments 18 has_many :special_comments, :order => 'type' 19 19 20 has_and_belongs_to_many :categories 21 has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id' 20 has_and_belongs_to_many :categories, :order => 'name' 21 has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id', :order => 'name DESC, type ASC' 22 22 23 23 has_many :taggings, :as => :taggable 24 24 has_many :tags, :through => :taggings, :include => :tagging do … … 38 38 has_many :categorizations, :foreign_key => :category_id 39 39 has_many :authors, :through => :categorizations 40 40 41 has_many :readers 42 has_many :people, :through => :readers 41 has_many :readers, :order => 'readers.person_id' 42 has_many :people, :through => :readers, :order => 'first_name DESC, id ASC' 43 43 44 44 def self.what_are_you 45 45 'a post...' -
activerecord/test/fixtures/author.rb
old new 1 1 class Author < ActiveRecord::Base 2 has_many :posts 3 has_many :posts_with_comments, :include => :comments, :class_name => "Post" 4 has_many :posts_with_categories, :include => :categories, :class_name => "Post" 2 has_many :posts, :order => 'title DESC' 3 has_many :posts_with_comments, :include => :comments, :class_name => "Post", :order => 'title DESC' 4 has_many :posts_with_categories, :include => :categories, :class_name => "Post", :order => 'title' 5 5 has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" 6 6 has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension 7 7 def testing_proxy_owner … … 14 14 proxy_target 15 15 end 16 16 end 17 has_many :comments, :through => :posts 18 has_many :funky_comments, :through => :posts, :source => :comments 17 has_many :comments, :through => :posts, :order => 'body DESC' 18 has_many :funky_comments, :through => :posts, :source => :comments, :order => 'body' 19 19 20 20 has_many :special_posts, :class_name => "Post" 21 21 has_many :hello_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'hello'" -
activerecord/lib/active_record/associations.rb
old new 1147 1147 add_conditions!(sql, options[:conditions], scope) 1148 1148 add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit]) 1149 1149 1150 sql << "ORDER BY #{options[:order]} " if options[:order] 1151 1150 sql << "ORDER BY #{options[:order]}" if options[:order] 1151 1152 # Collect all eagerly-loaded assocations with any order options, and 1153 # add in their table names unless it's already there. 1154 associated_orders = join_dependency.join_associations.select do |assoc| 1155 assoc.reflection.options[:order] 1156 end.map do |assoc| 1157 orders = assoc.reflection.options[:order].split(',').map(&:strip) 1158 orders.map do |order| 1159 if order =~ /\./ 1160 order 1161 else 1162 assoc.aliased_table_name + '.' + order 1163 end 1164 end 1165 end 1166 1167 # Add the association orders to the SQL statement. 1168 if associated_orders.any? 1169 if options[:order] 1170 sql << ", " 1171 else 1172 # Ordering by primary key is implicit in SQL, and is only overridden 1173 # when the order is explicit. Because a blank options[:order] is assumed 1174 # to sort by primary key, we must preserve this explicitly in the SQL. 1175 sql << "ORDER BY #{table_name}.#{primary_key}, " 1176 end 1177 sql << associated_orders.flatten.join(', ') 1178 end 1179 sql << ' ' 1180 1152 1181 add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections) 1153 1182 1154 1183 return sanitize_sql(sql)