rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Rails 6 deprecates where.not as NOR & Rails 6.1 as NAND

Open jsolas opened this issue 3 years ago • 6 comments

Currently if you use Rails 5.2 you can do for example: Post.where.not(source_type: "Feed", source_id: 100) and if we do the same in Rails 6.0.0.rc1 shows us the warning NOT conditions will no longer behave as NOR in Rails 6.1.To continue using NOR conditions, NOT each conditions manually.

This will make the migration process to Rails 6 difficult and it will not be correct to continue using the conditions as rails 5.2 does.

Describe the solution you'd like

Create a cop prohibiting the use of where.not with two arguments. I would like to contribute by creating it

Additional context

This is the Rails pull request where it was made https://github.com/rails/rails/pull/36029 More details of the change in this article https://www.bigbinary.com/blog/rails-6-deprecates-where-not-working-as-nor-and-will-change-to-nand-in-rails-6-1

jsolas avatar Sep 27 '21 15:09 jsolas

Post.where (source_type: "Feed", source_id: 100)

Did you mean to write Post.where.not(source_type: "Feed", source_id: 100) ?

andyw8 avatar Sep 27 '21 15:09 andyw8

An alternative way to enforce this would be to configure Rails to raise on deprecation warnings:

config.active_support.deprecation = :raise

andyw8 avatar Sep 27 '21 15:09 andyw8

Post.where (source_type: "Feed", source_id: 100)

Did you mean to write Post.where.not(source_type: "Feed", source_id: 100) ?

yes, sorry, fixed!

jsolas avatar Sep 27 '21 15:09 jsolas

@jsolas Do you already have started with implementing this cop or do you have time capacities in the near future for it? Otherwise I can take care about it.

@andyw8 Would you be willing to merge such a cop? I've seen you commented in https://github.com/rubocop/rails-style-guide/pull/296#issuecomment-998390640, that you consider a chained not as bad, too. So what do you expect from this cop?

denzelem avatar Jan 13 '22 15:01 denzelem

@denzelem A cop would surely be nice to have. A PR is welcome.

So what do you expect from this cop?

I'd say the cop should raise an offence for multiple attributes. And tolerate chained where.not as a configurable option.

pirj avatar Jan 13 '22 15:01 pirj

An alternative way to enforce this would be to configure Rails to raise on deprecation warnings:

    config.active_support.deprecation = :raise

It's not always feasible I suppose, e.g. if there are other deprecation warnings.

pirj avatar Jan 13 '22 15:01 pirj