From a3502c419e621c6abac2a9e047e42a582fd80769 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 16 Mar 2006 06:17:54 +0000 Subject: Use association's :conditions when eager loading. [jeremyevans0@gmail.com] closes #4144 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3897 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_go_eager_test.rb | 50 +++++++++++++++++++++++++ activerecord/test/fixtures/author.rb | 3 ++ activerecord/test/fixtures/categories_posts.yml | 4 ++ activerecord/test/fixtures/category.rb | 3 ++ 4 files changed, 60 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index 50cb9bec59..9eb1b96baf 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -224,7 +224,57 @@ class EagerAssociationTest < Test::Unit::TestCase post = Post.find(6, :include=>[ :monkeys, :elephants ]) } end + + def find_all_ordered(className, include=nil) + className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include) + end + def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm + # Eager includes of has many and habtm associations aren't necessarily sorted in the same way + def assert_equal_after_sort(item1, item2, item3 = nil) + assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) + assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3 + end + # Test regular association, association with conditions, association with + # STI, and association with conditions assured not to be true + post_types = [:posts, :hello_posts, :special_posts, :nonexistent_posts] + # test both has_many and has_and_belongs_to_many + [Author, Category].each do |className| + d1 = find_all_ordered(className) + # test including all post types at once + d2 = find_all_ordered(className, post_types) + d1.each_index do |i| + assert_equal(d1[i], d2[i]) + assert_equal_after_sort(d1[i].posts, d2[i].posts) + post_types[1..-1].each do |post_type| + # test including post_types together + d3 = find_all_ordered(className, [:posts, post_type]) + assert_equal(d1[i], d3[i]) + assert_equal_after_sort(d1[i].posts, d3[i].posts) + assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type)) + end + end + end + end + + def test_eager_with_multiple_associations_with_same_table_has_one + d1 = find_all_ordered(Firm) + d2 = find_all_ordered(Firm, :account) + d1.each_index do |i| + assert_equal(d1[i], d2[i]) + assert_equal(d1[i].account, d2[i].account) + end + end + + def test_eager_with_multiple_associations_with_same_table_belongs_to + firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition] + d1 = find_all_ordered(Client) + d2 = find_all_ordered(Client, firm_types) + d1.each_index do |i| + assert_equal(d1[i], d2[i]) + firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) } + end + end def test_eager_with_valid_association_as_string_not_symbol assert_nothing_raised { Post.find(:all, :include => 'comments') } end diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 3138dfca57..8c6cee51e4 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -4,6 +4,9 @@ class Author < ActiveRecord::Base has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" + has_many :special_posts, :class_name => "Post" + has_many :hello_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'hello'" + has_many :nonexistent_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'nonexistent'" has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding, :after_add => :log_after_adding, :before_remove => :log_before_removing, :after_remove => :log_after_removing diff --git a/activerecord/test/fixtures/categories_posts.yml b/activerecord/test/fixtures/categories_posts.yml index 0d77d8c05f..9b67ab4fa4 100644 --- a/activerecord/test/fixtures/categories_posts.yml +++ b/activerecord/test/fixtures/categories_posts.yml @@ -17,3 +17,7 @@ general_sti_habtm: sti_test_sti_habtm: category_id: 3 post_id: 6 + +general_hello: + category_id: 1 + post_id: 4 diff --git a/activerecord/test/fixtures/category.rb b/activerecord/test/fixtures/category.rb index 9be459a80f..6917c51d34 100644 --- a/activerecord/test/fixtures/category.rb +++ b/activerecord/test/fixtures/category.rb @@ -1,5 +1,8 @@ class Category < ActiveRecord::Base has_and_belongs_to_many :posts + has_and_belongs_to_many :special_posts, :class_name => "Post" + has_and_belongs_to_many :hello_posts, :class_name => "Post", :conditions => "\#{aliased_table_name}.body = 'hello'" + has_and_belongs_to_many :nonexistent_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'nonexistent'" def self.what_are_you 'a category...' -- cgit v1.2.3