-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Spurious Performance/Count violations in rails #1868
Comments
I find your usage of |
Funny you bring that up, it complains about that case too: For my original post, I simplified the example query so it's understandable. I agree you'd never do this with the user table in a real project, but that doesn't change the overall problem here. The actual query I'm using in my project is a bit more complex, and can't be easily done in the database. In addition, when following a relation, you might want to avoid a database query if you have all the data local already. Something like this:
|
If for |
Yep, here's the actual violation call if it helps debugging:
|
Thanks for reporting the bug, and sorry for any issues that it has caused you. This cop can be improved, but I do not think that it can be completely fixed. As @bbatsov said,
The cop, right now, does not have a safe guard for the params only being the As for the case of The use case from rails is
Active Record is kind of lazy in its implementation of
Unfortunately, Active Record does not implement the same logic for
I personally feel that Active Record should treat Since there are conditions that this cop cannot safely handle, I think that the options are to remove the cop, or modify it to only complain about the |
I think we should just fix the problem with |
…block or &:something format
…block or &:something format
[Fix #1868] Only register an offense in Count for select with block or &:something format
There are a number of scenarios where this usage impacts our applications too. I would speculate that many people manage their ActiveRecord::Relation execution like this, especially since Rails 4 has such good support for passing around unexecuted relations, and popular gems like While I recognise that the cop is unfixable currently, I might suggest disabling it by default as @rrosenblum suggested was one of the alternatives? Or restricting its capabilities even further, if it's possible to make a safe variant? |
Half the Rails checks have limitations, so I'd rather leave it to the users to decide which to use and which to leave out. I plan to dwell more on those some day, but they are generally low on my todo list. It's way more important for me to avoid positives in the universal Ruby checks. |
My mistake, I didn't realise this was a Rails-specific cop - I had assumed these were rails specific problems for an otherwise generic Ruby performance cop. |
The original intention for this cop was not to be a Rails specific cop. In implementation, it appears to not function well in the context of Rails. I can modify the cop to not register an offense when klass = user? User : Account
klass.select { |x| x.admin? == true }.count |
…r select called as a static method
…r select called as a static method
Hi! I use relation for unpersisted object on |
I just ran into the same problem @wli and my work around was to put everything up to the select in a separate variable, then ran that variable with .count on the next line to avoid the rubocop issues. Edited: I just realized that the count method can take in a block, so you don't need the select. Either way, both are good for rubocop. You can do this: Or with your other example: For example with your code above: Workaround: So in your other case: You could try this workaround: I hope this workaround helps if anyone else runs into this rubocop issue! |
Just upgraded to 0.31.0, and it looks like Performance/Count is complaining about my rails code. Specifically, a line like this:
User.select { |x| x.admin? == true }.count
will result in an error: "Use count instead of select...count"
However, the
.count
function in rails does not work with a block. You can see that nothing happens when I try to follow the suggestion:Is there a workaround for this, or do I need to turn off this cop for all rails projects?
cc @rrosenblum
The text was updated successfully, but these errors were encountered: