I have the following grails domain classes:
A {
belongsTo = [b: B]
}
B {
hasMany = [aClasses: A]
}
In a GSP view I have a form where the user can select many A instances to connect them with a B instance. When the user submits the form the Controller which handles the form receives something like this:
params: [aClasses: [123,124]]
The user may have removed previously added aClasses from B so I tried to clear the aClasses of B first and then perform addTo but I get a java.util.ConcurrentModificationException.
Here is what I do in my Controller Action:
def update() {
B b = B.get(params.id)
b.properties = params
b.aClasses.clear()
def newAs = params.aClassIds ? [] + params.aClasses : []
newAs.each {
A a = A.get(it)
b.addToAClasses(a)
}
b.save(flush: true)
}
Is there a way to make this work?