Validate presence of 1 of 2 fields

Seth Buntin wrote:

def validate
  errors.add("Either home email or work email need to be entered.")
unless home_email.nil? or work_email.nil?
end

Overriding validate directly isn't recommended. Try:

validate_on_create do |record|
  record.errors.add("", "Either home email or work email need to be
entered.") if record.home_email.blank? || record.work_email.blank?
end

J. Yaunches wrote:

validates_presence_of :home_email, :work_email, :message => "Either home
email or work email need to be entered."

That would trigger an error unless both are provided. I think Seth
wants an error only if none are given.

validate_on_create do |record|
  record.errors.add("", "Either home email or work email need to be
entered.") if record.home_email.blank? || record.work_email.blank?
end

Sorry change the "||" in the if statement to "&&"

validate_on_create do |record|
  record.errors.add("Home or work email", "need to be entered.") if
record.home_email.blank? && record.work_email.blank?
end

should work then. Your original 'unless' expression was saying they
both needed to be provided.