rubocop / rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.
https://docs.rubocop.org/rubocop-rails
MIT License
815 stars 263 forks source link

Error while Rails/UniqueValidationWithoutIndex cop was inspecting #1226

Open aarona opened 10 months ago

aarona commented 10 months ago

Expected behavior

Expected rubocop to run without errors.

Actual behavior

Received the following errors:

27 errors occurred:
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /home/.../app/models/message.rb:20:2.

# .... more similar errors on different lines.

Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.59.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.3.0) [x86_64-linux]

Steps to reproduce the problem

Every single error was related to lines where I'm using validates. Here are some examples:

  validates :name, :email, :phone, :body, presence: true
  validates :email, format: { with: /.+@.+\..+/ }, allow_blank: true
  validates :phone, format: { with: /\A(1)?(\d{3})?\d{7}(x\d{5})?\z/ }, allow_blank: true
  validates :contact_email, format: { with: /.+@.+\..+/ },
                            if: -> { contact_email.present? }
  validates :contact_daytime_phone,
            format: { with: /\A(1)?(\d{3})?\d{7}(x\d{5})?\z/ },
            if: -> { contact_daytime_phone.present? }
  validates :contact_cell_phone,
            format: { with: /\A(1)?(\d{3})?\d{7}(x\d{5})?\z/ },
            if: -> { contact_cell_phone.present? }
  validates :contact_fax,
            format: { with: /\A(1)?(\d{3})?\d{7}(x\d{5})?\z/ },
            allow_blank: true

It might be important to note this project doesn't have any ActiveRecord model classes and that these aren't actually * < ActiveRecord::Base classes but are POROs that include some Rails functionality so they behave like ActiveRecord classes. Here's an example:

class Message
  include ActiveModel::Model
  include ActiveModel::Validations::Callbacks

  # ... more class code

  def persisted?
    false
  end
end

Also note that I'm using ruby 3.3.0p0 as well. I'm assuming because of the text in the errors though that this might be a rubocop-rails bug and not necessarily a rubocop bug.

RuboCop version

$ [bundle exec] rubocop -V
1.59.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.3.0) [x86_64-linux]
  - rubocop-rails 2.23.1
koic commented 9 months ago

Unfortunately, I'm unable to reproduce the error. Rails/UniqueValidationWithoutIndex cop works model with db/schema.rb. Can you provide reproduction code for the error?

yashka713 commented 9 months ago

@koic Could you explain how rubocop works withschema.rb?

Error:

app/models/buyer_user_notifications_setting.rb:4:3: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.
  validates :buyer_user_id, uniqueness: true
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Model:

validates :buyer_user_id, uniqueness: true

Schema:

create_table "buyer_user_notifications_settings", force: :cascade do |t|
    ...
    t.index ["buyer_user_id"], name: "index_buyer_user_notifications_settings_on_buyer_user_id", unique: true
  end

Migration:

add_index :buyer_user_notifications_settings, :buyer_user_id, unique: true, if_not_exists: true

pg_index:

Screenshot 2024-01-05 at 15 41 00

Thank you!

aarona commented 9 months ago

Unfortunately, I'm unable to reproduce the error. Rails/UniqueValidationWithoutIndex cop works model with db/schema.rb. Can you provide reproduction code for the error?

I think that might be the issue here then. This is a simple website that actually has no ActiveRecord models associated with it. Just my POROs that are grabbing ActiveRecord functionality through some includes. Here's all of the code in my db/schema.rb file:

ActiveRecord::Schema.define(version: 0) do
end

Regardless, it seems that @yashka713 still has this problem even with real ActiveRecord models in their project.