views:

29

answers:

2

I'm totally blocked on this.

See this pastie for sample code: http://pastie.org/990040 The first test will fail.

The user model validates an associated address model, but is only supposed to do it if a flag is true. In practice it does it all the time.

What is going on?

A: 

Ok.

It seems this line: def validate_associated_records_for_address; end successfully overrides forced validation.

Apparently anaf compulsorily adds validations on associated models. Those validations call validate_associated_records_for_. By overiding that method to do nothing you turn off anafs mandatory validations and then validates_associated...:if=> can do its thing.

James Ferguson
A: 

Actually I ran into further problems with my (more complicated) real world scenario that were only solved by doing the equivalent of:

def validate_associated_records_for_address
  self.addressable? ? validate_single_association(User.reflect_on_association(:address)) : nil
end

This adapts anaf's compulsory validations to only run under the condition we want (addressable? is true).

The validates_associated...:if is not necessary now.

James Ferguson