public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Alias included associations if needed when doing a count

[#302 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Tarmo Tänav (author)
Tue Aug 26 09:00:47 -0700 2008
jeremy (committer)
Wed Aug 27 23:30:25 -0700 2008
commit  0ed29df6fa704349fd0af0d9521581d6a8eb109c
tree    14cc195b7e32977d602a6c858d7e5f08cd683dc2
parent  a3a3067adbd578ad9d145f5a148816ec0460a987
...
186
187
188
 
 
 
 
189
190
 
191
192
193
 
 
 
194
195
196
...
186
187
188
189
190
191
192
193
 
194
195
196
 
197
198
199
200
201
202
0
@@ -186,11 +186,17 @@ module ActiveRecord
0
             sql << " FROM (SELECT #{distinct}#{column_name}" if use_workaround
0
             sql << " FROM #{connection.quote_table_name(table_name)} "
0
           end
0
+
0
+          joins = ""
0
+          add_joins!(joins, options, scope)
0
+
0
           if merged_includes.any?
0
-            join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
0
+            join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, joins)
0
             sql << join_dependency.join_associations.collect{|join| join.association_join }.join
0
           end
0
-          add_joins!(sql, options, scope)
0
+
0
+          sql << joins unless joins.blank?
0
+
0
           add_conditions!(sql, options[:conditions], scope)
0
           add_limited_ids_condition!(sql, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
0
 
...
193
194
195
 
 
 
 
196
...
193
194
195
196
197
198
199
200
0
@@ -193,4 +193,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
0
     # due to Unknown column 'comments.id'
0
     assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
0
   end
0
+
0
+  def test_count_with_include_should_alias_join_table
0
+    assert_equal 2, people(:michael).posts.count(:include => :readers)
0
+  end
0
 end

Comments