Is it possible to have multiple entries for the :on option for the
validates validation?
Something like the following:
class Library < ActiveRecord::Base
validates :presence => true, :on => [:create, :update, :some_other_action]
end
Basically I would like to run one particular validation when multiple
actions are called in the controller. For example, I would like to
validate some user info on the create action, on a update action,
and possibly some other actions as well.
Although I don’t want the validation to run on all the methods.
Also, if there is an easier way of doing this, that would be great too!
Thanks in advance!
Yes for create and update!
:on – Specifies when this validation is active. Runs in all validation contexts by default (nil), other options are :create and :update
However think of these values as database data create or update statements, and not rails controller actions. They may be doing similar things to data and the database, but they are different.
For example you may have another rails method that also updates the database, perhaps updating different fields, however the above ‘:update’ will still account for that method even though it’s not coming from a rails action with the name ‘update’.