views:

44

answers:

2

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

A: 

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
Voyta
I like your style, and I agree it that should work (I tried something quite similar in my tests), but any time you iterate over tasks from the list object in the model, none of the tasks will be deleted. I've tried this with both 2.3.8 and 3.0.0beta4 to no avail
serpico7456
It is strange, I tested with Rails 2.3.8, and it works. One thing - records are deleted from database, but not removed from `list.tasks` cached collection. So it can appear as if tasks have not been deleted. You can try `list.tasks.reload` if you need to access your object after update.
Voyta
A: 

I ended up extending Magazine's save method to get around the problem. It worked like a charm.

def save
  saved = false
  ActiveRecord::Base.transaction do
    saved = super
    if self.conditions.size < 1
      saved = false
      errors[:base] << "A rule must have at least one condition."
      raise ActiveRecord::Rollback
    end
  end
  saved
end
serpico7456