aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/calculations.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-11-07 15:07:39 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-11-07 15:07:39 +0000
commit37adea6ff1fba85c29406bbe2af4831f4c6beeec (patch)
tree3bfbd9e8b81f554bd9e6893e0ed93040065aa0d5 /activerecord/lib/active_record/calculations.rb
parent31e2a2d9bbe3a375912223c133ba793b72600a9f (diff)
downloadrails-37adea6ff1fba85c29406bbe2af4831f4c6beeec.tar.gz
rails-37adea6ff1fba85c29406bbe2af4831f4c6beeec.tar.bz2
rails-37adea6ff1fba85c29406bbe2af4831f4c6beeec.zip
Address shortcomings of changeset [8054] [protocool]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8109 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/calculations.rb')
-rw-r--r--activerecord/lib/active_record/calculations.rb19
1 files changed, 4 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 9620ce1b20..7f8af9421e 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -15,11 +15,8 @@ module ActiveRecord
# The third approach, count using options, accepts an option hash as the only parameter. The options are:
#
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
- # * <tt>:joins</tt>: Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
- # or names associations in the same form used for the :include option.
- # If the value is a string, then the records will be returned read-only since they will have attributes that do not correspond to the table's columns.
- # Pass :readonly => false to override.
- # See adding joins for associations under Association.
+ # * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
+ # The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
# * <tt>:include</tt>: Named associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
# to already defined associations. When using named associations count returns the number DISTINCT items for the model you're counting.
# See eager loading under Associations.
@@ -112,9 +109,7 @@ module ActiveRecord
# Person.minimum(:age, :conditions => ['last_name != ?', 'Drake']) # Selects the minimum age for everyone with a last name other than 'Drake'
# Person.minimum(:age, :having => 'min(age) > 17', :group => :last_name) # Selects the minimum age for any family without any minors
def calculate(operation, column_name, options = {})
- options, ar_joins = *extract_ar_join_from_options(options)
validate_calculation_options(operation, options)
- options[:ar_joins] = ar_joins if ar_joins
column_name = options[:select] if options[:select]
column_name = '*' if column_name == :all
column = column_for column_name
@@ -154,14 +149,8 @@ module ActiveRecord
operation = operation.to_s.downcase
options = options.symbolize_keys
- scope = scope(:find)
- if scope && scope[:ar_joins]
- scope = scope.dup
- options = options.dup
- options[:ar_joins] = scope.delete(:ar_joins)
- end
+ scope = scope(:find)
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
- merged_includes = merge_includes(merged_includes, options[:ar_joins])
aggregate_alias = column_alias_for(operation, column_name)
if operation == 'count'
@@ -184,7 +173,7 @@ module ActiveRecord
sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround
sql << " FROM #{connection.quote_table_name(table_name)} "
if merged_includes.any?
- join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins], options[:ar_joins])
+ join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
end
add_joins!(sql, options, scope)