Given the following code:
class MyClass < ActiveRecord::Base
has_many :technical_contacts has_many :schedule_contacts
validates_associated :technical_contacts, :schedule_contacts
protected
def validate validate_contacts(technical_contacts) validate_contacts(schedule_contacts) end
private
def validate_contacts(contacts) contacts.each_with_index{|contact, i| (i+1).upto(contacts.length-1) do |j| if contacts[j].email == contact.email contacts[j].errors.add(:email, 'You have already provided this email address') end end } end
end
If my validate_contacts method adds errors to some contact objects, I would expect @myclass.save to fail but it does not. I have tested that the objects are invalid using the "valid?" method and they are so I don't understand what is going wrong. Can anyone please help?