aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-07 06:29:31 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-07 06:29:31 +0000
commit7b37c779d805081aedb635a8bc801aa4a4339cc2 (patch)
tree4f158d402df6ede6085bbced53a108447e7fd07f /activerecord/lib
parent4ab40059b7925f35cddb0f76dba27ea6cd018de7 (diff)
downloadrails-7b37c779d805081aedb635a8bc801aa4a4339cc2.tar.gz
rails-7b37c779d805081aedb635a8bc801aa4a4339cc2.tar.bz2
rails-7b37c779d805081aedb635a8bc801aa4a4339cc2.zip
Fixed counter_sql when no records exist in database for PostgreSQL (would give error, not 0) #1039 [Caleb Tennis]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1104 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index b4f1b3ba73..c27bcecdd6 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -426,8 +426,14 @@ module ActiveRecord #:nodoc:
# Product.count "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
def count_by_sql(sql)
sql = sanitize_conditions(sql)
- count = connection.select_one(sql, "#{name} Count").values.first
- return count ? count.to_i : 0
+ rows = connection.select_one(sql, "#{name} Count")
+
+ if rows.nil?
+ return 0
+ else
+ count = rows.values.first
+ return count ? count.to_i : 0
+ end
end
# Increments the specified counter by one. So <tt>DiscussionBoard.increment_counter("post_count",