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:29:29 -0700 2008
commit  13671cc565aad2327f81a29789154b829ceeda04
tree    9fcca45f5d322423f71563f91b11dbfa84f01c92
parent  ad562c58eabfb8b44cb8ac9e87b87a7f998325fd
...
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
 
...
196
197
198
 
 
 
 
199
...
196
197
198
199
200
201
202
203
0
@@ -196,4 +196,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