Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created August 4, 2010 03:42
Show Gist options
  • Save mitchellh/507599 to your computer and use it in GitHub Desktop.
Save mitchellh/507599 to your computer and use it in GitHub Desktop.
# Validates that a given collection is properly populated.
#
# Usage:
#
# class Person
# has_many :assets
# validates :assets, :populated_collection => true
# end
#
class PopulatedCollectionValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
keep_results = value.select { |o| !o.marked_for_destruction? }
record.errors.add(attribute, options[:message] || "can't be empty.") if keep_results.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment