From b8657089f289e2b3a9d5c383516e951012b2bec2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Sep 2007 23:50:12 +0000 Subject: [PATCH] Eager loading respects explicit :joins. Closes #9496. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7494 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations.rb | 2 +- activerecord/test/associations/eager_test.rb | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index dee50dbb3a..92e0cb642d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Eager loading respects explicit :joins. #9496 [dasil003] + * Extract Firebird, FronBase, and OpenBase adapters into gems. #9508, #9509, #9510 [Jeremy Kemper] * RubyGem database adapters: expects a gem named activerecord--adapter with active_record/connection_adapters/_adapter.rb in its load path. [Jeremy Kemper] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index adde221477..8cb6bd02ac 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1266,7 +1266,7 @@ module ActiveRecord def construct_finder_sql_for_association_limiting(options, join_dependency) scope = scope(:find) - is_distinct = include_eager_conditions?(options) || include_eager_order?(options) + is_distinct = !options[:joins].blank? || include_eager_conditions?(options) || include_eager_order?(options) sql = "SELECT " if is_distinct sql << connection.distinct("#{table_name}.#{primary_key}", options[:order]) diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb index bbf3edd0c9..fa632bdf69 100644 --- a/activerecord/test/associations/eager_test.rb +++ b/activerecord/test/associations/eager_test.rb @@ -103,6 +103,11 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal [2], posts.collect { |p| p.id } end + def test_eager_association_loading_with_explicit_join + posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id') + assert_equal 1, posts.length + end + def test_eager_with_has_many_through posts_with_comments = people(:michael).posts.find(:all, :include => :comments) posts_with_author = people(:michael).posts.find(:all, :include => :author )