The easiest way to use :if as follows:
class User < ActiveRecord::Base validate_presence_of :name validate_presence_of :age, :if => Proc.new { |user| user.signup_step >= 2 } # ... etc end
or
class User < ActiveRecord::Base validate_presence_of :name validate_presence_of :age, :if => :registering? def registering? signup_step >= 2 end end
You can also use the validate method to determine any complex, custom logic. For example:
class User < ActiveRecord::Base validate :has_name_and_email_after_invitation validate :has_complete_profile_after_registration def has_name_and_email_after_invitation if ...
(In the example above, you can really define validation rules in has_name_and_email_after_invitation with regular calls validates_xxx_of , because they should also be applied in step 2, but using two methods for the individual steps gives you maximum flexibility.)
molf
source share