Skip to content

Instantly share code, notes, and snippets.

@boone
Created January 30, 2011 03:49
Show Gist options
  • Save boone/802504 to your computer and use it in GitHub Desktop.
Save boone/802504 to your computer and use it in GitHub Desktop.
validation method dealing with _destroy attribute from accepts_nested_attributes_for
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