-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Conversation
I think we should just remove options argument from this method. Otherwise |
That would definitely simplify things @meinac and it would be closer to how this was finally dealt with on master. But wouldn't it break existing code that relies on this feature with the help of the activerecord-deprecated_finders gem? |
@rousisk IMHO in activerecord-deprecated_finders gem side we can create a |
@meinac I totally agree with you - this would be a cleaner way. This would require some kind of coordination in gem version dependencies, so that users don't find themselves with a newer rails version while still using the current Also this PR's code would never make it to It would be nice if @matthewd, @rafaelfranca or @sgrif could weigh in. |
Hi @matthewd, @rafaelfranca, @sgrif. Is there any interest in this PR ? |
Hi @rafaelfranca, just rebased and pushed. Let me know if I can be of any help. |
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." |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 😁
There was a problem hiding this comment.
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 :)
Please rebase. |
@maclover7 done! |
…assed to Relation#count
@maclover7 had to rebase again. Let me know if you need anything else. |
@rousisk I'm not a member of the Rails core team (only an Issue team member) so you may have to wait a little while to get a merge or more feedback... |
Sorry for the delay. I think it is better to be handled in the gem that in Rails. We could just leave the code as it is here and as soon people upgrade the deprecated finders gem they will get the proper errors. |
Ah, it still need to changes here. So yeah, we can change Rails to not accept the options argument. It is not different of the current implementation since it gives an argument error when the gem is not present. |
thanks for the feedback @rafaelfranca. So you suggest changing the definition to We won't have any more the descriptive error message for people that just upgraded, is that OK? |
Guard against finder options or invalid-type column names in Relation#count
This is what I came up with regarding #20434 . Keep in mind that the current implementation of
count
in 4.1 and 4.2 has two related but distinct issues:count(:all)
is performed.I introduced an explicit check for the first case, raising an
ArgumentError
whenever an options hash is specified. When theactiverecord-deprecated_finders
gem is present we can safely let that handle the finder options.I handle the second issue by passing the argument to the DB directly and letting that to eventually raise an error. This is consistent with the approach taken in the current
master
when provided with an invalid type for a column name.Providing both a column name and an options hash
These are the results of running
User.count(:id, conditions: { username: 'rousis' })
:Providing a single hash argument
And these are the results of running
User.count({ username: 'rousis' })
:This PR targets
4-2-stable
but it might be a useful addition to4-1-stable
as well.Any feedback on the concept or the implementation would be greatly appreciated.