I have the following domain objects:
class Foo {
List<Bar> bars
static hasMany = [bars: Bars]
static mapWith = 'mongo'
}
class Bar {
Foo foo
static belongsTo = [foo: Foo]
static mapWith = 'mongo'
}
The following code in a service:
Bar bar1 = new Bar('bar1')
Bar bar2 = new Bar('bar2')
Bar bar3 = new Bar('bar3')
foo.addToBars(bar1)
foo.addToBars(bar2)
foo.addToBars(bar3)
foo.save
And the following code in a controller:
def foo = Foo.findAll()
def bars = foo.bars
Why is bars a nested array?
I get something like: [['bar1','bar2','bar3']]
I am expecting something like ['bar1','bar2','bar3']
More info:
The problem was that I was doing .bars on the object returned by findAll. I changed it to findOne() and everything worked as expected.
Solution:
def foo = Foo.findOne() // was: def foo = Foo.findAll()
def bars = foo.bars