Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against finder options or invalid-type column names in Relation#count #20450

Merged
merged 1 commit into from
Mar 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* ActiveRecord::Relation#count: raise an ArgumentError when finder options
are specified or an ActiveRecord::StatementInvalid when an invalid type
is provided for a column name (e.g. a Hash).

Fixes #20434

*Konstantinos Rousis*

* Fix regression when loading fixture files with symbol keys.

Closes #22584.
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ module Calculations
# Note: not all valid +select+ expressions are valid +count+ expressions. The specifics differ
# between databases. In invalid cases, an error from the database is thrown.
def count(column_name = nil, options = {})
if options.present? && !ActiveRecord.const_defined?(:DeprecatedFinders)
raise ArgumentError, "Relation#count does not support finder options anymore. " \
"Please build a scope and then call count on it or use the " \
"activerecord-deprecated_finders gem to enable this functionality."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we should add this code, as the deprecated finders won't be supported in Rails 5: https://github.com/rails/activerecord-deprecated_finders

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kaspth, this is targeted to 4.2 only, not master! Its purpose being to ease upgrades from 4.0 to more recent versions of 4.x. See also #20434 for background/context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right it is! So used to things being against master I didn't think twice about it. Sorry about that, and thanks for working on this 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, better safe than sorry so thanks for looking at it in the first place :)


end

# TODO: Remove options argument as soon we remove support to
# activerecord-deprecated_finders.
column_name, options = nil, column_name if column_name.is_a?(Hash)
calculate(:count, column_name, options)
end

Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,14 @@ def test_count_with_too_many_parameters_raises
assert_raise(ArgumentError) { Account.count(1, 2, 3) }
end

def test_count_with_a_single_hash_parameter_raises
assert_raise(ActiveRecord::StatementInvalid) { Account.count({}) }
end

def test_count_with_finder_options_raises
assert_raise(ArgumentError) { Account.count(:firm_name, { conditions: {} }) }
end

def test_count_with_order
assert_equal 6, Account.order(:credit_limit).count
end
Expand Down