HOw do I perform this conditional validation?

Hi,

I have this validation rule to check if the user-entered date of birth
is at least 18 years of age …

validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, “You must be at least 18 years old to
use this site.” if value > Date.new((Date.today.year - 18),
(Date.today.month),(Date.today.day))
end

Now I want to refine this so it validation only occurs if this
condition above is true and the user_type_id virtual attribute is
equal to one. How do I add this additional piece of logic into my
validation rule?

Thanks, - Dave

On Sep 7, 11:42 pm, “[email protected]
[email protected] wrote:

Now I want to refine this so it validation only occurs if this
condition above is true and the user_type_id virtual attribute is
equal to one. How do I add this additional piece of logic into my
validation rule?

Just add record.user_type_id == 1 to that condition.

Fred

I guess I’m slow. How would that look with the code above? -

On Sep 8, 1:23 am, Frederick C. [email protected]

On 8 Sep 2008, at 15:05, [email protected] wrote:

I guess I’m slow. How would that look with the code above? -

just add it to the if you’ve got.

Fred

Thanks. I think I’m still doing something wrong because I now have

validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, “You must be at least 18 years old to
use this site.” if :user_type_id != 1 && value >
Date.new((Date.today.year - 18),(Date.today.month),(Date.today.day))
end

but I’m getting an error upon trying to create my object. The error
is

NoMethodError in Super admin/subscriberController#create
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.>

app/models/user.rb:10
app/controllers/super_admin/subscriber_controller.rb:43:in `create’

Request

Parameters:

{“user”=>{“pharmacy_id”=>“333”,
“work_phone_extension”=>“”,
“credit_card_number”=>“”,
“user_type_id”=>“1”,
“ship_to_last_name”=>“Alvarado”,
“ship_to_city”=>“Denver”,
“credit_card_expiration_date(1i)”=>“2008”,
“credit_card_expiration_date(2i)”=>“9”,
“ship_to_country”=>“US”,
“password_confirmation”=>“password”,
“credit_card_security_code”=>“”,
“ship_to_street”=>“1201 Galapago “,
“ship_to_zip”=>“80204”,
“credit_card_expiration_date(3i)”=>“8”,
“bill_to_city”=>””,
“credit_card_company_id”=>“1”,
“phone”=>“512-222-3333”,
“fax”=>“”,
“bill_to_last_name”=>“”,
“bill_to_street”=>“”,
“ship_to_first_name”=>“Dave”,
“ship_to_state”=>“CO”,
“login”=>“bigpharma”,
“password”=>“password”,
“bill_to_state”=>“”,
“ship_to_company”=>“Dave Pharm”,
“email”=>“[email protected]”,
“bill_to_first_name”=>“”,
“bill_to_zip”=>“”},
“commit”=>“Create”}

On Sep 8, 8:19 am, Frederick C. [email protected]