From 2c6e616b90483681ccaff7e04d47b88470a309b0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 13 Mar 2008 18:41:42 +0000 Subject: Fixed that scoped joins would not always be respected (closes #6821) [Theory/Danger] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9022 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 15 ++++++++------- activerecord/test/cases/associations/join_model_test.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8026f24023..645ecdcda6 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that scoped joins would not always be respected #6821 [Theory/Danger] + * Ensure that ActiveRecord::Calculations disambiguates field names with the table name. #11027 [cavalle] * Added add/remove_timestamps to the schema statements for adding the created_at/updated_at columns on existing tables #11129 [jramirez] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a30036f561..7de760b28e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1478,13 +1478,14 @@ module ActiveRecord #:nodoc: # The optional scope argument is for the current :find scope. def add_joins!(sql, options, scope = :auto) scope = scope(:find) if :auto == scope - join = (scope && scope[:joins]) || options[:joins] - case join - when Symbol, Hash, Array - join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, join, nil) - sql << " #{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join} " - else - sql << " #{join} " + [(scope && scope[:joins]), options[:joins]].each do |join| + case join + when Symbol, Hash, Array + join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, join, nil) + sql << " #{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join} " + else + sql << " #{join} " + end end end diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 810e477ac0..1034cb67e9 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -299,6 +299,14 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_equal [], posts(:thinking).authors assert_equal [authors(:mary)], posts(:authorless).authors end + + def test_both_scoped_and_explicit_joins_should_be_respected + assert_nothing_raised do + Post.send(:with_scope, :find => {:joins => "left outer join comments on comments.id = posts.id"}) do + Post.find :all, :select => "comments.id, authors.id", :joins => "left outer join authors on authors.id = posts.author_id" + end + end + end def test_belongs_to_polymorphic_with_counter_cache assert_equal 0, posts(:welcome)[:taggings_count] -- cgit v1.2.3