When working with Grails, I like to rely on automatic data binding and scaffolding as much as possible. I have the following problem. I have a Flow domain class that has a collection of instances of a Node domain class, which is the last 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 stream using data binding, 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. This makes sense because Grails cannot create an instance of node [0], since Node is an abstract class, but the questions are:
- Is there a way to specify a specific Node class so that it can be created?
Technically, this is completely possible (in fact, Grails already does something similar to save and retrieve instances using the classname column), but I'm not sure if this case has already been considered in data binding. If not:
- What would you consider the best solution without touching the controller?
source share