aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rwxr-xr-xactiverecord/test/base_test.rb12
3 files changed, 16 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 84b7f61f0f..a8a1bf0ad6 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed extraneous comma in count() function that made it not work with joins #1156 [jarkko/Dee.Zsombor]
+
* Fixed incompatibility with Base#find with an array of ids that would fail when using eager loading #1186 [Alisdair McDiarmid]
* Fixed that validate_length_of lost :on option when :within was specified #1195 [jhosteny@mac.com]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 8136e101f4..c11b6d23ed 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -438,9 +438,8 @@ module ActiveRecord #:nodoc:
# Returns the number of records that meets the +conditions+. Zero is returned if no records match. Example:
# Product.count "sales > 1"
def count(conditions = nil, joins = nil)
- tbl_var_name = joins ? table_name[0,1].downcase : ""
- sql = "SELECT COUNT(*) FROM #{table_name} #{tbl_var_name} "
- sql << ", #{joins} " if joins
+ sql = "SELECT COUNT(*) FROM #{table_name} "
+ sql << " #{joins} " if joins
add_conditions!(sql, conditions)
count_by_sql(sql)
end
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 1792cf799c..f0f2682bb1 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -11,6 +11,7 @@ class Category < ActiveRecord::Base; end
class Smarts < ActiveRecord::Base; end
class CreditCard < ActiveRecord::Base; end
class MasterCreditCard < ActiveRecord::Base; end
+class Post < ActiveRecord::Base; end
class LoosePerson < ActiveRecord::Base
attr_protected :credit_rating, :administrator
@@ -777,4 +778,15 @@ class BasicsTest < Test::Unit::TestCase
k.set_inheritance_column { original_inheritance_column + "_id" }
assert_equal "type_id", k.inheritance_column
end
+
+ def test_count_with_join
+ res = Post.count_by_sql "SELECT COUNT(*) FROM posts LEFT JOIN comments ON posts.id=comments.post_id WHERE posts.type = 'Post'"
+ res2 = res + 1
+ assert_nothing_raised do
+ res2 = Post.count("posts.type = 'Post'",
+ "LEFT JOIN comments ON posts.id=comments.post_id")
+ end
+ assert_equal res, res2
+ end
+
end