title: Validating nested models?
body: <p>To be more specific, <strong><em>"How do I validate that a model requires at least x valid associated models to be created?"</em></strong>. I've been trying to validate nested models that get created in the same form as the parent (and ultimately show immediate validations a la jQuery). As a popular example, lets assume the following models and schema.</p>
  
  <pre><code>class Project
    include DataMapper::Resource
  
    property :id,     Serial
    property :title,  String, :nullable =&gt; false
  
    has 2..n, :tasks
  end
  
  class Task
    include DataMapper::Resource
  
    property :id,         Serial
    property :project_id, Integer,  :key =&gt; true
    property :title,      String,   :nullable =&gt; false
  
    belongs_to :project
  end
  </code></pre>
  
  <p>All the validations are done in the schema definitions, as you can see. The important one here is "<code>has 2..n, :tasks</code>". This validation actually works normally, given that the nested task attributes in the params hash will produce <em>valid</em> tasks. If they produce an <em>invalid</em> task, however, then the task won't get created and you'll end up with a Project that has less than 2 tasks, and thus an <em>invalid</em> project object.</p>
  
  <p>As I understand it, this is because it can't figure out if the task attributes are valid or not until it attempts to save the tasks, and since - <em>as far as I know</em> - the tasks can't get saved before the project, the project is unaware if the tasks will be valid or not. Am I correct in assuming this?</p>
  
  <p>Anyway, I was hoping there would be a quick answer, but it seems a lot less trivial then I'd hoped. If you've got any suggestions at all, that would be greatly appreciated.</p>