-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
Rails/WhereEquals autocorrect changes query generation behavior #403
Comments
Possibly due to #362 @eugeneius ? |
The query string has changed, but I think the result will not changed. |
In this case there is no |
That's interesting. Is it possible to provide (minimum) executable reproducible code and Rails version? It's possible that Active Record behaves unintentionally. Anyway, if it's difficult to resolve false positives for incompatible behavior, we can consider the unsafe |
Thanks @koic - here's a minimal Rails 4.2 project which shows off the difference: https://github.com/yitznewton/rubocop-where-equal-issue
|
Thank you for the additional information. RuboCop Rails is conservative about support for Rails 4.2. The RuboCop Rails 3.0 will drop support for Rails 4.2 in near future. So, I'd like to see if there is a similar feedback in Rails 5 (Preferably maintained 5.2 or higher). |
@koic I replaced the repo with a Rails 5.2 version at the same URL; the behavior is still observed. |
I found a related issue (I can file this separately if you prefer). The following Active Record model
Produces the output:
But in this case
which can be shortened to
What rubocop did caused double encoding in our Rails app. Because of this encoding corner case difference, automatic fix is not always safe. But it was very helpful to spot unnecessary manual SQL! |
btw @yitznewton in your example, this would have worked right?
joins affect how |
Fixes rubocop#403 and rubocop#418. This PR marks `Rails/WhereEquals` as unsafe auto-correction.
Fixes rubocop#403 and rubocop#418. This PR marks `Rails/WhereEquals` as unsafe auto-correction.
…utocorrect [Fix #403] Mark `Rails/WhereEquals` as unsafe auto-correction
Fix applied automatically with `bundle exec rubocop --auto-correct-all --only Rails/WhereEquals`. I then reran `bundle exec rubocop --auto-correct` to automatically fix up some introduced formatting problems. > This cop identifies places where manually constructed SQL in `where` can be replaced with `where(attribute: value)`. > > This cop’s autocorrection is unsafe because is may change SQL. See: rubocop/rubocop-rails#403 - https://docs.rubocop.org/rubocop-rails/cops_rails.html#railswhereequals - https://www.rubydoc.info/gems/rubocop-rails/2.9.1/RuboCop/Cop/Rails/WhereEquals
On updating 2.8.1 to 2.9.0, running autocorrect on a particular piece of code results in Rails incorrectly inferring the table of a column (
vendors.staged
), and generating a different Postgres query for the wrong table. In this case the code itself can be fixed after autocorrect by specifying the table name.Active Record code before:
SQL before:
Active Record after:
SQL after:
RuboCop version
Before
After
The text was updated successfully, but these errors were encountered: