title: Auto Binding a Grails One-To-Many Relationship
body: <p>I'm having difficulty auto-binding a one-to-many relationship in Grails without resorting to some hack in the controller. I understand that a one to many relationship in Grails is a set which is unordered and somehow affects binding.</p>
  
  <p>When I save this form, sometimes the data saves correctly, and sometimes it does not. If an author has 3-4 books, it seems that it works less often.</p>
  
  <p>In this example, I've tried to remove all non-relevant code to illustrate the issue.</p>
  
  <p>Models:</p>
  
  <pre><code>class Author {
      String name
      static hasMany = [ books:Book ]
  }
  
  class Book {
      String title
      static belongsTo = [ author:Author ]
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;g:form method="post" class="form-horizontal"&gt;
      &lt;g:hiddenField name="id" value="${authorInstance?.id}" /&gt;
      &lt;g:hiddenField name="version" value="${authorInstance?.version}" /&gt;
      &lt;g:textField name='name' value='${authorInstance?.name}'/&gt;
      &lt;g:each var="book" in="${authorInstance.books}" status="i"&gt;
          &lt;g:hiddenField name='book[${i}].id' value='${book.id}'/&gt;
          &lt;g:textField name='book[${i}].title' value='${book.title}'/&gt;
      &lt;/g:each&gt;
      &lt;g:actionSubmit action="update" value="Update" /&gt;
  &lt;/g:form&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>def update(Long id, Long version) {
      def author = Author.get(id)
  
      // removed "Not Found" and "Version" validation for this example
  
      author.properties = params
  
      if (!author.save(flush: true)) {
          render(view: "edit", model: [author: author])
          return
      }
  
      flash.message = "Success"
      redirect(action: "list"
  }
  </code></pre>
  
  <p>How can I structure my model and view so I can leave the controller relatively untouched?</p>