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

Better handling of negative words in enum #39065

Closed
inkstak opened this issue Apr 27, 2020 · 4 comments · Fixed by #40679
Closed

Better handling of negative words in enum #39065

inkstak opened this issue Apr 27, 2020 · 4 comments · Fixed by #40679
Labels

Comments

@inkstak
Copy link

inkstak commented Apr 27, 2020

Actual behavior

Given the models:

class Task < ApplicationRecord
  enum status: %i[pending completed not_completed]
end

class Query < ApplicationRecord
  enum status: %i[pending completed not_implemented]
end

They will generates the following warnings:

# Task
An enum element in Task uses the prefix 'not_'. This will cause a conflict with auto generated negative scopes.
Creating scope :not_completed. Overwriting existing method Task.not_completed.

# Query
An enum element in Query uses the prefix 'not_'. This will cause a conflict with auto generated negative scopes.

It will also generate the two scopes:

Query.not_implemented
Query.not_not_implemented

Expected behavior

  1. The warning

The warning added by #36380 can be quite enigmatic, if not useless, when there are no conflicts in the enumeration.

Does it mean we should ban not_ words from enum ?
I don't think so.

The method detect_negative_condition used in active_record/enum.rb#L203 should check the whole enum collection to detect potential conflicts.

  1. The scopes

Could we generate an implemented scope instead of a not_not_implemented ?

I will be happy to propose a pull request (or two) after receiving some opinions.

System configuration

Rails version: 6.0.2.2

Ruby version: 2.6.3

@rails-bot
Copy link

rails-bot bot commented Jul 26, 2020

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-0-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@ghiculescu
Copy link
Member

#40679

@inkstak
Copy link
Author

inkstak commented Nov 25, 2020

Thanks @ghiculescu.
Tell me if i am wrong, your PR is just fixing the warning issue ?
What do you think about the not_not_ scopes ?

@ghiculescu
Copy link
Member

Yeah that’s correct. I’m happy to do both; for our app I just wanted the warning gone.

ghiculescu added a commit to ghiculescu/rails that referenced this issue Dec 9, 2020
Fixes rails#39065. Adds a more descriptive warning, and logs it less often.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants