title: How to make Groovy / Grails return a List of objects instead of a List of Lists of objects?
body: <p>I have a class like this:</p>
  
  <pre><code>class Foo {
      static hasMany = [bars: Bar]
  }
  </code></pre>
  
  <p>When I write:</p>
  
  <pre><code>Foo.getAll()
  </code></pre>
  
  <p>I get a list of <code>Foo</code> objects like this:</p>
  
  <pre><code>[ Foo1, Foo2, Foo3 ]
  </code></pre>
  
  <p>When I write:</p>
  
  <pre><code>Foo.getAll().bars
  </code></pre>
  
  <p>I get a list of lists of <code>Bar</code> object like this:</p>
  
  <pre><code>[ [ Bar1, Bar2 ], [ Bar2, Bar3 ], [ Bar1, Bar4 ] ] 
  </code></pre>
  
  <p>But what I want is a unique list of <code>Bar</code> objects like this:</p>
  
  <pre><code>[ Bar1, Bar2, Bar3, Bar4 ]
  </code></pre>
  
  <p>My end goal is to have a unique list of ids of the <code>Bar</code> object in the list above, like this:</p>
  
  <pre><code>[ 1, 2, 3, 4 ]
  </code></pre>
  
  <p>I have tried variations of the <a href="http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#collect%28groovy.lang.Closure%29">collect</a> method and I have also tried the <a href="http://groovy.codehaus.org/Operators#Operators-SpreadOperator">spread operator</a> but I'm not having any luck.</p>