aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
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",