2

I had this on my models:

validates :deal_sector_id,
              presence: true,
              numericality: { only_integer: true }

deal_sector_id is a integer postgresql column.

I'm starting to doubt that numericality: { only_integer: true } is necessary in my validations, as when I upgraded to shoulda-matchers 3.0, I get this error message :

You are attempting to use validate_numericality_of, but the attribute
       you're testing, :deal_sector_id, is an integer column. One of the
       things that the numericality matcher does is to assert that setting
       :deal_sector_id to a string that doesn't look like an integer will
       cause your deal to become invalid. In this case, it's impossible to
       make this assertion since :deal_sector_id will typecast any incoming
       value to an integer. This means that it's already guaranteed to be
       numeric! Since this matcher isn't doing anything, you can remove it
       from your model tests, and in fact, you can remove the validation from
       your model as it isn't doing anything either.

As you read above, it says "... and in fact, you can remove the validation from your model as it isn't doing anything either."

Should I use numericality: { only_integer: true } on 'integer' database tables' columns?

Mathieu
  • 4,587
  • 11
  • 57
  • 112
  • 2
    Nope, those absolutely no reason to do that, because your database will handle those validations for you. If you try and save anything that is non-integer into your database, Postgres will kick back an error for you. – Catherine Cai Oct 25 '15 at 00:17

1 Answers1

3

The validation helper numericality is intended for String values (VARCHAR from Postgres), and it will match them against a regular expression.

You're therefore correct in saying that it's not required for native Integer values from the DB.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
tompave
  • 11,952
  • 7
  • 37
  • 63