Skip to content

Instantly share code, notes, and snippets.

@asux
Created August 25, 2011 13:05
Show Gist options
  • Save asux/1170601 to your computer and use it in GitHub Desktop.
Save asux/1170601 to your computer and use it in GitHub Desktop.
class ExeclyOneValidator < ActiveModel::Validator
def validate(record)
raise ArgumentError, ":fields option doesn't supplied" unless options.has_key?(:fields)
if options[:fields].select { |f| record.read_attribute(f.to_s).present? }.size != 1
_fields = options[:fields].map(&:to_s).map(&:titleize).join(", ")
record.errors[:base] << "execly one of #{_fields} must be present"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment