Skip to content

Commit

Permalink
Alias included associations if needed when doing a count
Browse files Browse the repository at this point in the history
[#302 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
tarmo authored and jeremy committed Aug 28, 2008
1 parent a3a3067 commit 0ed29df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions activerecord/lib/active_record/calculations.rb
Expand Up @@ -186,11 +186,17 @@ def construct_calculation_sql(operation, column_name, options) #:nodoc:
sql << " FROM (SELECT #{distinct}#{column_name}" if use_workaround
sql << " FROM #{connection.quote_table_name(table_name)} "
end

joins = ""
add_joins!(joins, options, scope)

if merged_includes.any?
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, joins)
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
end
add_joins!(sql, options, scope)

sql << joins unless joins.blank?

add_conditions!(sql, options[:conditions], scope)
add_limited_ids_condition!(sql, options, join_dependency) if join_dependency && !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])

Expand Down
Expand Up @@ -193,4 +193,8 @@ def test_dynamic_find_should_respect_association_include
# due to Unknown column 'comments.id'
assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
end

def test_count_with_include_should_alias_join_table
assert_equal 2, people(:michael).posts.count(:include => :readers)
end
end

0 comments on commit 0ed29df

Please sign in to comment.