Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate presence of nested attributes

How do I validate that a model has at least one associated model using nested attributes? This has been driving me crazy as I am sure that I am missing something simple. For example, I want to require that a List always has at least one Task.

class List < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy
  accepts_nested_attributes_for :tasks, :allow_destroy => true
end

class Task < ActiveRecord::Base
  belongs_to :list
end

I've tried many different options.

1- adding a validation to lists:

def validate
  if self.tasks.length < 1
    self.errors[:base] << "A list must have at least one task."
  end
end

but this will still allow you to delete all the tasks of an existing list since when deleting tasks the validation of list happens before the tasks are destroyed.

2- checking to see if any tasks are not marked for destruction in a before_save callback

before_save :check_tasks

private
#look for any task which won't be deleted
def check_tasks
  for t in self.tasks
    return true if ! t.marked_for_destruction?
  end
  false  
end

For some reason I can't get it to ever delete a task with anything that iterates over a list's tasks. The same is true if I do this check in def validate instead of a callback

3- requiring the presence of tasks validates_presence_of :tasks, but with this it won't ever delete any tasks

like image 324
Ryan Horrisberger Avatar asked Dec 23 '22 01:12

Ryan Horrisberger


1 Answers

You can check both conditions together in validation method:

  validate :check_tasks
  def check_tasks
    if self.tasks.size < 1 || self.tasks.all?{|task| task.marked_for_destruction? }
      errors.add_to_base("A list must have at least one task.")
    end
  end
like image 72
Voyta Avatar answered Jan 08 '23 03:01

Voyta