aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/base.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-02-25 23:06:04 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-02-25 23:06:04 +0000
commit99307b959b1c16ab1dd0400a7398019fe9d5494b (patch)
tree94a082a3c7f91c8a98596a8bcf29f2c99d4baa70 /activerecord/lib/active_record/base.rb
parent1aea4704dca570e3ef7abe69349ae2f161c7244b (diff)
downloadrails-99307b959b1c16ab1dd0400a7398019fe9d5494b.tar.gz
rails-99307b959b1c16ab1dd0400a7398019fe9d5494b.tar.bz2
rails-99307b959b1c16ab1dd0400a7398019fe9d5494b.zip
Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Base.maxmium, and the generic Base.calculate. All can be used with :group and :having. Calculations and statitics need no longer require custom SQL. #3958 [Rick Olson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3646 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-xactiverecord/lib/active_record/base.rb55
1 files changed, 0 insertions, 55 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 681f99b5c0..dfa9bfab9d 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -495,61 +495,6 @@ module ActiveRecord #:nodoc:
connection.delete(sql, "#{name} Delete all")
end
- # Count operates using three different approaches.
- #
- # * Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
- # * Count by conditions or joins: For backwards compatibility, you can pass in +conditions+ and +joins+ as individual parameters.
- # * Count using options will find the row count matched by the options used.
- #
- # The last 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>: 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.
- #
- # Examples for counting all:
- # Person.count # returns the total count of all people
- #
- # Examples for count by +conditions+ and +joins+ (for backwards compatibility):
- # Person.count("age > 26") # returns the number of people older than 26
- # Person.find("age > 26 AND job.salary > 60000", "LEFT JOIN jobs on jobs.person_id = person.id") # returns the total number of rows matching the conditions and joins fetched by SELECT COUNT(*).
- #
- # Examples for count with options:
- # Person.count(:conditions => "age > 26")
- # Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
- # Person.count(:conditions => "age > 26 AND job.salary > 60000", :joins => "LEFT JOIN jobs on jobs.person_id = person.id") # finds the number of rows matching the conditions and joins.
- def count(*args)
- options = {}
-
- #For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}).
- if args.size >= 0 and args.size <= 2
- if args.first.is_a?(Hash)
- options = args.first
- #should we verify the options hash???
- else
- #Handle legacy paramter options: def count(conditions=nil, joins=nil)
- options.merge!(:conditions => args[0]) if args.length > 0
- options.merge!(:joins => args[1]) if args.length > 1
- end
- else
- raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
- end
-
- options[:include] ? count_with_associations(options) : count_by_sql(construct_counter_sql(options))
- end
-
- def construct_counter_sql(options)
- sql = "SELECT COUNT("
- sql << "DISTINCT " if options[:distinct]
- sql << "#{options[:select] || "#{table_name}.#{primary_key}"}) FROM #{table_name} "
- sql << " #{options[:joins]} " if options[:joins]
- add_conditions!(sql, options[:conditions])
- sql
- end
-
# Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
def count_by_sql(sql)