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 occurred Rails/UniqueValidationWithoutIndex #228

Closed
katelovescode opened this issue Apr 5, 2020 · 2 comments
Closed

Error occurred Rails/UniqueValidationWithoutIndex #228

katelovescode opened this issue Apr 5, 2020 · 2 comments

Comments

@katelovescode
Copy link

An error was thrown while running rubocop -a. All my cops pass.


Expected behavior

Not throw an error.

Actual behavior

All cops pass, but an error is thrown.

1 error occurred:
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /Users/katedonaldson/Projects/pieforproviders/app/models/user.rb:11:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.80.1 (using Parser 2.7.0.5, running on ruby 2.6.5 x86_64-darwin19)

Steps to reproduce the problem

Backtrace for this error:

.Scanning /Users/katedonaldson/Projects/pieforproviders/app/models/user.rb
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /Users/katedonaldson/Projects/pieforproviders/app/models/user.rb:11:2.
undefined method `type' for nil:NilClass
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:82:in `each_content'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `each'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `map'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `build_columns'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:51:in `initialize'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:27:in `new'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:27:in `block in build!'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:37:in `block in each_table'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:34:in `each'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:34:in `each_table'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:26:in `build!'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:12:in `initialize'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:34:in `new'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:34:in `load!'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:18:in `load'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/cop/rails/unique_validation_without_index.rb:128:in `schema'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-rails-2.5.0/lib/rubocop/cop/rails/unique_validation_without_index.rb:35:in `on_send'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:57:in `block (2 levels) in trigger_responding_cops'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:136:in `with_cop_error_handling'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:56:in `block in trigger_responding_cops'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:55:in `each'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:55:in `trigger_responding_cops'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:32:in `block (2 levels) in <class:Commissioner>'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/ast/traversal.rb:57:in `block in on_begin'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/ast/traversal.rb:57:in `each'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/ast/traversal.rb:57:in `on_begin'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:33:in `block (2 levels) in <class:Commissioner>'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/ast/traversal.rb:99:in `on_class'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:33:in `block (2 levels) in <class:Commissioner>'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/ast/traversal.rb:14:in `walk'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:44:in `investigate'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/team.rb:124:in `investigate'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/team.rb:112:in `offenses'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:296:in `inspect_file'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:244:in `block in do_inspection_loop'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:276:in `block in iterate_until_no_changes'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:269:in `loop'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:269:in `iterate_until_no_changes'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:240:in `do_inspection_loop'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:119:in `block in file_offenses'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:143:in `file_offense_cache'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:117:in `file_offenses'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:108:in `process_file'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:87:in `block in each_inspected_file'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:86:in `each'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:86:in `reduce'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:86:in `each_inspected_file'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:73:in `inspect_files'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/runner.rb:39:in `run'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli/command/execute_runner.rb:21:in `execute_runner'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli/command/execute_runner.rb:13:in `run'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli/command.rb:10:in `run'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli/environment.rb:17:in `run'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli.rb:65:in `run_command'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/lib/rubocop/cli.rb:41:in `run'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/exe/rubocop:13:in `block in <top (required)>'
/Users/katedonaldson/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/gems/rubocop-0.80.1/exe/rubocop:12:in `<top (required)>'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/bin/rubocop:23:in `load'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/bin/rubocop:23:in `<main>'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/bin/ruby_executable_hooks:24:in `eval'
/Users/katedonaldson/.rvm/gems/ruby-2.6.5@pieforproviders/bin/ruby_executable_hooks:24:in `<main>'

The line that seems to throw the error is validates :email, presence: true, uniqueness: true in user.rb

# frozen_string_literal: true

# Application users
class User < ApplicationRecord
  # Handles UUIDs breaking ActiveRecord's usual ".first" and ".last" behavior
  self.implicit_order_column = 'created_at'

  has_many :businesses, dependent: :restrict_with_error

  validates :active, inclusion: { in: [true, false] }
  validates :email, presence: true, uniqueness: true
  validates :full_name, presence: true
  validates :language, presence: true
  validates :opt_in_email, inclusion: { in: [true, false] }
  validates :opt_in_phone, inclusion: { in: [true, false] }
  validates :opt_in_text, inclusion: { in: [true, false] }
  validates :service_agreement_accepted, inclusion: { in: [true, false] }
  validates :timezone, presence: true

  scope :active, -> { where(active: true) }

  # format phone numbers - remove any non-digit characters
  def phone=(value)
    super(value.blank? ? nil : value.gsub(/[^\d]/, ''))
  end
end

Here's my .rubocop.yml:

require: rubocop-rails

AllCops:
  Exclude:
    - db/**/**
    - bin/**
    - Guardfile
    - vendor/**/*

Naming/FileName:
  Exclude:
    - config/initializers/**

Metrics/BlockLength:
  Exclude:
    - spec/**/**
    - lib/tasks/**

Layout/LineLength:
  Max: 180

Rails:
  Enabled: true

Style/ClassAndModuleChildren:
  EnforcedStyle: compact
  Exclude:
    - config/application.rb

Style/Encoding:
  Enabled: false

Style/HashEachMethods:
  Enabled: true

Style/HashTransformKeys:
  Enabled: true

Style/HashTransformValues:
  Enabled: true

RuboCop version

0.80.1 (using Parser 2.7.0.5, running on ruby 2.6.5 x86_64-darwin19)
@koic koic transferred this issue from rubocop/rubocop Apr 5, 2020
@koic
Copy link
Member

koic commented Apr 5, 2020

I think this error has been probably resolved by #214. Can you upgrade RuboCop Rails to 2.5.1?
https://rubygems.org/gems/rubocop-rails/versions/2.5.1

@katelovescode
Copy link
Author

Yep, that did it! Thank you very much!

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

2 participants