Reputation: 1761
When working with Grails, I like to rely on automatic databinding and scaffolding as much as possible. I have the following problem. I have a domain class Flow that have a collection of instances of the domain class Node, being the latest one an abstract class:
class Flow {
List nodes
static hasMany = [ nodes: Node]
static constraints = { nodes minSize:1 }
}
abstract class Node {
String title
static belongsTo = [ ownerFlow: Flow]
}
There are several classes that inherit from Node. When trying to create a Flow using databinding the following integration test fails:
void "a flow can be created from a single request using the default grails data binding"() {
when:
def controller = new FlowController()
controller.request.addParameters(['nodes[0].title': 'First step'])
new Flow(controller.params).save(flush:true)
then:
Flow.count() > 0
}
}
The moment I change Node from abstract to non-abstract, the test passes. It makes total sense because Grails is not capable of create the node[0] instance, since Node is an abstract class, but the questions is:
Technically is perfectly possible (in fact Grails is already doing something similar for persisting and retrieving the instances by using a classname column), but I'm not sure if this case is already considered in the databinding. If not:
Upvotes: 4
Views: 520
Reputation: 7985
You need to configure a default for the purposes of binding:
List nodes = [].withDefault { new MyConcreteNode() }
Upvotes: 6