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

validate_uniqueness_of boolean column #1075

Closed
aaronmallen opened this issue Jan 26, 2018 · 1 comment · Fixed by #1136
Closed

validate_uniqueness_of boolean column #1075

aaronmallen opened this issue Jan 26, 2018 · 1 comment · Fixed by #1136
Milestone

Comments

@aaronmallen
Copy link

Getting the following error when trying to conditionally validate a boolean value:

NoMethodError:
       undefined method `attribute_setter' for nil:NilClass

Given an environment with:

$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
$ rails -v
Rails 5.1.4

# Gemfile.lock
shoulda-matchers (3.1.2)
rspec-core (3.7.1)

given a class with a boolean column

# foo.rb
#
# == Schema Information
#
# Table name: foo
#
#  id               :integer    not null, primary key
#  some_bool_val    :boolean    not null
class Foo < ApplicationRecord
  validates :some_bool_val, inclusion: { in: [true, false], allow_nil: false, allow_blank: false },
                            uniqueness: { if: :some_bool_val }
end

and a spec

# foo_spec.rb

RSpec.describe Foo, type: :model do
  # this test passes
  context 'with some_bool_val true' do
    subject { create(:foo, some_bool_val: true) }
    it { should validate_uniqueness_of :some_bool_val }
  end

  # this test produces the error
  context 'with some_bool_value false' do
    subject { create(:foo, some_bool_val: false) }
    it { should_not validate_uniqueness_of :some_bool_val }
  end
end

full trace:

Failures:

  1) Foo with some_bool_value false should not validate that :some_bool_value is case-sensitively unique
     Failure/Error: it { should_not validate_uniqueness_of :some_bool_value }

     NoMethodError:
       undefined method `attribute_setter' for nil:NilClass
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher.rb:405:in `failure_message'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:69:in `failure_message_when_negated'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:133:in `failure_reason_when_negated'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb:346:in `failure_reason_when_negated'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:68:in `block in failure_message_when_negated'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:67:in `tap'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:67:in `failure_message_when_negated'
     # ./spec/models/foo_spec.rb:67:in `block (3 levels) in <top (required)>'
     # ./spec/support/database_cleaner.rb:12:in `block (3 levels) in <top (required)>'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/database_cleaner-1.6.2/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/database_cleaner-1.6.2/lib/database_cleaner/base.rb:98:in `cleaning'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/database_cleaner-1.6.2/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
     # $HOME/.rvm/gems/ruby-2.5.0@my-project/gems/database_cleaner-1.6.2/lib/database_cleaner/configuration.rb:87:in `cleaning'
     # ./spec/support/database_cleaner.rb:11:in `block (2 levels) in <top (required)>'

Finished in 3.62 seconds (files took 3.53 seconds to load)
165 examples, 1 failure, 1 pending

Failed examples:

rspec ./spec/models/foo_spec.rb:67 # Foo with some_bool_value false should not validate that :some_bool_value is case-sensitively unique

Randomized with seed 53832
@mcmire
Copy link
Collaborator

mcmire commented Jan 26, 2018

@aaronmallen Thanks for reminding me about this. A lot of people have reported this -- #904 contains a bunch of references -- so if you don't mind I'm going to close this in favor of that. I have a branch going that fixes this bug and provides more information in case of a failure, so I'll have that done soon.

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

Successfully merging a pull request may close this issue.

2 participants