Polymorphic Validations

Hi Everyone,

Ran into an interesting case last night, and I was hoping that someone
on the list here could give me some guidance.

I’ve got a Polymorphic association for Addresses (which can belong to
two different models).

The catch is, one model requires a strict, precise address, and the
other only requires a City, State and Postal Code.

I want to use :validates_presence_of :street, :city, :state, and
:postal_code, but only if the instance is being assigned to the model
requiring stricter address validation.

Any thoughts?

-Jared

Jared wrote:

I’ve got a Polymorphic association for Addresses (which can belong to
two different models).

The catch is, one model requires a strict, precise address, and the
other only requires a City, State and Postal Code.

I want to use :validates_presence_of :street, :city, :state, and
:postal_code, but only if the instance is being assigned to the model
requiring stricter address validation.

You can pass an :if => :strict_address_required? parameter to each
relevant validates_presence_of call, and then define a method that
looks like:

def strict_address_required?
if addressable_type == “Person”
true
else
false
end
end

Chris