aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-26 17:02:45 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-26 17:02:45 +0000
commita0bf0195a5c27c8c8493b1a44987154a9dc0d526 (patch)
tree9c48753be9e25512a3dd58fde92af218fb0f5db7 /activerecord/lib
parentc554a9cabaa3c8bbed5a0dccb381fdc6d516972d (diff)
downloadrails-a0bf0195a5c27c8c8493b1a44987154a9dc0d526.tar.gz
rails-a0bf0195a5c27c8c8493b1a44987154a9dc0d526.tar.bz2
rails-a0bf0195a5c27c8c8493b1a44987154a9dc0d526.zip
Deprecation: count class method should be called with an options hash rather than two args for conditions and joins. Closes #6287.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb2
-rw-r--r--activerecord/lib/active_record/calculations.rb37
2 files changed, 24 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 226698611c..d0af68eea7 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -137,7 +137,7 @@ module ActiveRecord
elsif @reflection.options[:counter_sql]
@reflection.klass.count_by_sql(@counter_sql)
else
- @reflection.klass.count(@counter_sql)
+ @reflection.klass.count(:conditions => @counter_sql)
end
@target = [] and loaded if count == 0
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index e0ac96c879..aab0fa2a74 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -9,7 +9,7 @@ module ActiveRecord
# 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 by conditions or joins: This API has been deprecated and will be removed in Rails 2.0
# * 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:
@@ -29,7 +29,7 @@ module ActiveRecord
# Examples for counting all:
# Person.count # returns the total count of all people
#
- # Examples for count by +conditions+ and +joins+ (for backwards compatibility):
+ # Examples for count by +conditions+ and +joins+ (this has been deprecated):
# 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(*).
#
@@ -128,24 +128,33 @@ module ActiveRecord
def construct_count_options_from_legacy_args(*args)
options = {}
column_name = :all
- # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}).
- if args.size >= 0 && args.size <= 2
- if args.first.is_a?(Hash)
- options = args.first
+
+ # We need to handle
+ # count()
+ # count(options={})
+ # count(column_name=:all, options={})
+ # count(conditions=nil, joins=nil) # deprecated
+ if args.size > 2
+ raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}"
+ elsif args.size > 0
+ if args[0].is_a?(Hash)
+ options = args[0]
elsif args[1].is_a?(Hash)
- options = args[1]
- column_name = args.first
+ column_name, options = args
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
+ # Deprecated count(conditions, joins=nil)
+ ActiveSupport::Deprecation.warn(
+ "You called count(#{args[0].inspect}, #{args[1].inspect}), which is a deprecated API call. Instead you should use " +
+ "count(column_name, options). Passing the conditions and joins as string parameters will be removed in Rails 2.0."
+ )
+ options.merge!(:conditions => args[0])
+ options.merge!(:joins => args[1]) if args[1]
end
- else
- raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
end
+
[column_name, options]
end
-
+
def construct_calculation_sql(operation, column_name, options) #:nodoc:
scope = scope(:find)
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])