Reputation: 11811
The numericality validator does not seem to work with attributes declared with the Attributes API
class Tester
include ActiveModel::Model
include ActiveModel::Attributes
attribute :units, :integer
validates :units, numericality: true
end
t = Tester.new(units: 'ABC')
t.valid? # => true
t.errors.full_messages # => []
I expected t
to be invalid because ABC
is not a number. This works if Tester is an ActiveRecord::Base. (Rails 5)
Upvotes: 0
Views: 50
Reputation: 106882
There is no error because ActiveModel::Attributes
casts the value to the defined type.
In your case 'ABC'
is cast to integer and 'ABC'.to_i #=> 0
. And 0
is obviously a number.
You can check that by checking the value of t.units
after running your example code.
Upvotes: 1