0

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?

Nick
  • 2,924
  • 4
  • 36
  • 43

1 Answers1

1

Long story short, on: :create does not work in this context.

Usually validations are not tied to actions anyways, so you're good with just removing it.

Have a good one, Jan

jfornoff
  • 1,368
  • 9
  • 15