I have a has_many association that accepts nested attributes. I need for there to be a minimum of 1 associated object in the collection, so I wrote a custom va
Create validator:
class CollectionLengthValidator < ActiveModel::Validations::LengthValidator
def validate_each(record, attribute, value)
value = value.respond_to?(:reject) ? value.reject(&:marked_for_destruction?) : value
super(record, attribute, value)
end
end
This is resolve problem with delete last record in collection.
Then use in model as standard length validator:
validates :foos, :collection_length => {:minimum => 1}