Skip to content

Better handling of negative words in enum #39065

@inkstak

Description

@inkstak

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions