Help with validates_presence_of

I’m trying to specify specific instances when ‘validates_presence_of’
should be used in a particular model. I looked in the docs and found
that you can set :on to specify when the particular validation should
occur. However, I’m not sure how that should be formatted.

What I ultimately want to do is use the validation when I am creating a
record, but not when I am updating it.

Could someone help me out with the format here. My attempt (shown
below) doesn’t work.

validates_presence_of :email, :password, { :on => :save, :new }

Thanks!
Sarah

Per documentation (api.rubyonrails.org)…

on - Specifies when this validation is active (default is :save, 

other
options :create, :update)

So change :new to :create and see what happens. If you still have
problems, then try making it two separate statements, as in…

validates_presence_of :email, :on => :save, :create
validates_presence_of :password, :on => :save, :create

c.

sarah_hutchinson wrote:

I’m trying to specify specific instances when ‘validates_presence_of’
should be used in a particular model. I looked in the docs and found
that you can set :on to specify when the particular validation should
occur. However, I’m not sure how that should be formatted.

What I ultimately want to do is use the validation when I am creating a
record, but not when I am updating it.

Could someone help me out with the format here. My attempt (shown
below) doesn’t work.

validates_presence_of :email, :password, { :on => :save, :new }

Thanks!
Sarah

Sorry - clicked submit too quickly… you don’t want the :save in
there…

validates_presence_of :email, :password, :on => :create

or

validates_presence_of :email, :on => :create
validates_presence_of :password, :on => :create

should work.
c.

Cayce B. wrote:

Per documentation (api.rubyonrails.org)…

on - Specifies when this validation is active (default is :save, 

other
options :create, :update)

So change :new to :create and see what happens. If you still have
problems, then try making it two separate statements, as in…

validates_presence_of :email, :on => :save, :create
validates_presence_of :password, :on => :save, :create

c.

sarah_hutchinson wrote:

I’m trying to specify specific instances when ‘validates_presence_of’
should be used in a particular model. I looked in the docs and found
that you can set :on to specify when the particular validation should
occur. However, I’m not sure how that should be formatted.

What I ultimately want to do is use the validation when I am creating a
record, but not when I am updating it.

Could someone help me out with the format here. My attempt (shown
below) doesn’t work.

validates_presence_of :email, :password, { :on => :save, :new }

Thanks!
Sarah

Figured I’d follow this up myself since I found the answer myself. The
correct format should have been:

validates_presence_of :vf_email, :password, :on => :create

Cheers,
Sarah

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs