title: How to save multiple rows of data by looping through an array or a map?
body: <pre><code>Class Buyer {
     String name
     static constraints = {
     }
  }
  Class Order {
      String ref
      static belongsTo = [buyer:Buyer]
      static constraints = {
      buyer(nullable:false)
      }
  }
  </code></pre>
  
  <p>In  OrderController.groovy</p>
  
  <pre><code>...
  def someAction = {
    //working
    def data1 = ["buyer.id": 2, "ref": "xyz"]
    def ord = new Order(data1);
    ord.save();
  
    def data2 = ["buyer.id": 2, "ref": "234xyz"]
    def ord2 = new Order(data2);
    ord2.save();
  
  
    //But in a loop - its not working
    def items = ['abc', 'def', 'ghi', 'jkl']
    def data2 = [:]
    for(e in items) {
       data2 = ["buyer.id": 2, "ref" : e.value] //keeping buyer id same
       def ord = new Order(data2);
       ord.save();
       data2 = [:] //just emptying it?
    }
  }
  </code></pre>
  
  <p>As you would notice in "working" above, if I am able to save multiple rows by copy pasting and definging new maps but If I try to loop through an array, it doesnt work. Any ideas how do I save data by looping through an array or a map? </p>
  
  <p>Any questions, please let know
  Thanks</p>