Created
January 30, 2011 03:49
-
-
Save boone/802504 to your computer and use it in GitHub Desktop.
validation method dealing with _destroy attribute from accepts_nested_attributes_for
This file contains hidden or 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
class Project < ActiveRecord::Base | |
has_many :tasks | |
accepts_nested_attributes_for :tasks, :reject_if => :all_blank, :allow_destroy => true | |
def validate | |
# require a minimum of one task | |
undestroyed_task_count = 0 | |
tasks.each { |t| undestroyed_task_count += 1 unless t.marked_for_destruction? } | |
errors.add_to_base 'There must be at least one task' if undestroyed_task_count < 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment