(Nanyang, I've read back through the thread, so I hope the following
will work).
Basically you'll want to set 3 varieties of validation. and within
your User model you'll an instance method to indicate the validation
you require.
within the controller (you may need 1 controller per type of
validation, or a way to figure out what validation method you'll use):
user = User.new(params[:user])
user.set_validation(User::GENERAL_INFO) #see model below [or
User::PERSONAL_INFO), User.set_validation(User::CONTACT_INFO]
if user.valid?
user.save
else
#deal with the validation error
end
and within your model, define the constants (validation methods,
actual validations, et al.)
class Users < ActiveRecord::Base
validates_presence_of :city, :if => Proc.new {|model| ! model.
contact_info_validation?}
GENERAL_INFO = 1
PERSONAL_INFO = 2
CONTACT_INFO = 3
def set_validation(means)
@validation_means = means
end
def contact_info_validation?
@validation_means == User::CONTACT_INFO
end
The above syntax isn't tested, so take the approach run with it.
Note: others with more Ruby Fu may recommend better ways to manage
constants (maybe as symbols), but this approach in general will scale
your requirements well.
Cheers,
Jodi
General Partner
The nNovation Group inc.
www.nnovation.ca/blog