Skip to content
Browse files

Fixed extraneous comma in count() function that made it not work with…

… joins #1156 [jarkko/Dee.Zsombor]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1262 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 54f2d1d commit eefe4d0ddd077f5e27caf8bfb5d306ed6cc86ed8 @dhh dhh committed Apr 30, 2005
Showing with 16 additions and 3 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +2 −3 activerecord/lib/active_record/base.rb
  3. +12 −0 activerecord/test/base_test.rb
View
2 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]
View
5 activerecord/lib/active_record/base.rb
@@ -438,9 +438,8 @@ def delete_all(conditions = nil)
# 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
View
12 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 @@ def test_set_inheritance_column_with_block
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

0 comments on commit eefe4d0

Please sign in to comment.
Something went wrong with that request. Please try again.