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
This commit is contained in:
David Heinemeier Hansson 2005-04-30 15:45:15 +00:00
parent 54f2d1d944
commit eefe4d0ddd
3 changed files with 16 additions and 3 deletions

View File

@ -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 File

@ -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

View File

@ -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