0

I am trying to validate on the model that my salary attr, an integer value, fits within a range, despite the value indeed fitting within that range, it fails with salary: ["is not included in the list"], any ideas what I'm missing:

model.rb

validates_inclusion_of :salary, :in => [1000..1000000]

passing specs:

it { should validate_presence_of(:salary) }
it { should_not allow_value(100).for(:salary) }
it { should_not allow_value(10000000).for(:salary) }

failing spec:

it { should allow_value(900000).for(:salary) }

900000 is within 1000..1000000 so any ideas why this still fails?

Thanks

jbk
  • 1,911
  • 19
  • 36
  • 1
    Can you give this a try? `validates_inclusion_of :salary, :in => 1000..1000000` To clarify: Your array doesn't include all the valid values, it include one value that is a range. – tbuehlmann Jun 20 '17 at 13:52
  • 1
    Oh dear , yup, that was it, thanks @tbuehlmann. Should the remarkable happen and someone be as daft as me I'll leave this post up here for their help. Please submit the above comment as an answer and I'll accept it. Thanks – jbk Jun 20 '17 at 13:55
  • 1
    Good to hear that it works. :) – tbuehlmann Jun 20 '17 at 13:59

1 Answers1

2

You are providing a range as the only valid value for the salary, which is not what you want. Using the following will do:

validates_inclusion_of :salary, :in => 1000..1000000
tbuehlmann
  • 9,032
  • 5
  • 27
  • 33