I have created a model (called PhoneNumber) that is not backed by a database table:
class PhoneNumber
include ActiveModel::Validations
attr_accessor :pnumber
validates :pnumber, presence: true, on: :create #=> { :message => " cannot be blank" }
validates :pnumber, numericality: true, on: :create
end
I am testing it in the console like this:
2.0.0-p451 :001 > example = PhoneNumber.new
=> #<PhoneNumber:0x007f839c90c690>
2.0.0-p451 :002 > example.valid?
=> true
2.0.0-p451 :003 > example.pnumber
=> nil
As you can see, the empty PhoneNumber is considered valid even if the :pnumber attribute is nil, i.e. the validation is not working. How to fix it?