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

Error while Rails/UniqueValidationWithoutIndex cop was inspecting #1226

Open
aarona opened this issue Dec 30, 2023 · 3 comments
Open

Error while Rails/UniqueValidationWithoutIndex cop was inspecting #1226

aarona opened this issue Dec 30, 2023 · 3 comments

Comments

@aarona
Copy link

aarona commented Dec 30, 2023

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
Copy link
Member

koic commented Jan 3, 2024

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
Copy link

yashka713 commented Jan 5, 2024

@koic Could you explain how rubocop works with schema.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
Copy link
Author

aarona commented Jan 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants