Skip to content

Instantly share code, notes, and snippets.

@mikeauclair
Created August 20, 2010 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeauclair/540840 to your computer and use it in GitHub Desktop.
Save mikeauclair/540840 to your computer and use it in GitHub Desktop.
module AllIfAny
def validates_all_if_any(fields, options={})
fields = [fields].flatten
all_fields = fields.dup
validates_each(fields, options) do |record, attr_name, value|
if !(all_fields.find_all{|f| !record.send(f).to_s.empty?}.empty?)
record.errors.add(attr_name, (options[:message] || 'is invalid')) if value.to_s.empty?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment