5

Hi all I have seen several issues similar to mine but none of the solutions seem to solve my issue so after a day and a half of trying everything I've decided to try my luck posting my problem.

I have a table which in a MySQL db with this model:

class Client< ActiveRecord::Base

  validates :name, :length => {:maximum => 255}, :presence => true
  validates :client_status, inclusion: { in: 0..2, :presence => true }
  validates :client_type, inclusion: { in: 0..2, :presence => true}
end

So I want the client_status and the client_type to only be numeric values between 0 and 2, here is the rspec I wrote to accommodate this:

describe Client do
  before do
    @client = Client.new
  end

  it "should allow name that is less than 255 characters" do
    long_char = 'a' *254
    @client.name = long_char
    @client.client_status = 0
    @client.client_type = 1
    @client.should be_valid
  end

end

This is a pretty simple test, I have presence true for both client_status and client_type so I must add those in the RSPEC, however running this rspec gives me this error message:

got errors: Value type is not included in the list, Status is not included in the list

I tried this, to see what the output is:

puts "client type is: #{@client.client_type} and status is: #{@client.client_status} ."

I received this output:

client type is: false and status is:  .

Thank you for reading this long post and I really hope someone can shed some light on this issue for me.

Note: I have changed the names of the model/rspec and some of the fields so that I do not violate my companies NDA.

Regards, Surep

Surep
  • 179
  • 4
  • 19
  • 1
    How does the schema for this table look like? In particular, what are the types of client_type and client_status? Are they really integers? – Jakob S Oct 08 '14 at 20:21
  • 1
    try changing `inclusion: { in: 0..2, :presence => true }` to `inclusion: { in: 0..2}, :presence => true` – xlembouras Oct 08 '14 at 20:31
  • @Jakob S - The fields are both TINYINT – Surep Oct 08 '14 at 20:41
  • @xlembouras - I have tried this and it produces the same error – Surep Oct 08 '14 at 20:42
  • 1
    I have tried your code on a blank project, and I cannot reproduce what you've reported. The code you have posted works as you expect with Ruby 2.0, Rails 4.1, RSpec 3.1 at least. – Jakob S Oct 09 '14 at 07:04
  • 1
    Sounds like the attributes are set somewhere in your code automatically. Search your Client model for `client_status` and `client_type` and watch out for `before_validation` hooks. Look at potential setters, maybe even `assign_attributes` was overridden. – Patrick Oscity Oct 09 '14 at 11:59
  • 1
    What does your `puts` "debug statement" output before and after the line `@client.should be_valid`, respectively? – Patrick Oscity Oct 09 '14 at 12:00
  • @p11y - I was able to indeed fix the issue thanks to your comment; I realized that in the migration for the table I created the data type tinyint by specifying client_type and client_status as boolean. This was something I found online. I changed the data types to integer with another migration and this made the test pass. Not sure which post to give the answer too but seeing as you helped me realize this thank you very much p11y and everyone else who commented. – Surep Oct 09 '14 at 14:17

2 Answers2

7
  1. In rails you need to divide validators by comma:
    validates :client_status, presence: true, inclusion: { in: 0..2 }
  1. It has no sense to check presence, if you check on inclusion. So you can simplify your code by simple validation:
    validates :client_status, inclusion: { in: 0..2 }
Alexander Karmes
  • 2,438
  • 1
  • 28
  • 34
1

I think you should use numericality: like this:

validates :client_status, numericality: { only_integer: true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 2 }, :presence => true
Fred Perrin
  • 1,124
  • 2
  • 17
  • 23
  • I tried this and got the error 'got errors: Client type can't be blank, Client status can't be blank' To add to this the puts statement is still showing false and ' ' respectively. – Surep Oct 08 '14 at 20:45