Created
August 4, 2010 03:42
-
-
Save mitchellh/507599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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