(Sorry if this is a noob question, I couldn't find the answers on the grails reference)
I have the following domain heirarchy :
User > (has many) Survey > (has many) SurveyQuestion > (has many) SurveyQuestionResponse
These are two of the above :
class Survey {
String surveyName
static hasMany = [questions: SurveyQuestion]
static belongsTo = [user:User]
static constraints = {
}
}
class SurveyQuestion {
String question
static hasMany = [responses : SurveyQuestionResponse]
static belongsTo = [survey:Survey]
static constraints = {
}
}
When I create a Survey, I first see a screen like this :
I fill in the survey name, then click add a survey question, and see the next screen :
But it requires a survey being set, which hasn't yet completed.
Question : Do I have to create and save the survey first, then edit it and add survey questions (each of which individually need to be created and saved before I can create responses), or is there a way to add child objects as I'm creating the parent objects?
I want to use dynamic scaffolding so I don't have to create controllers and views manually.
The questions and answers are entirely independent, and will not be re-used across the hierarchy.
You should use command objects. This way you can comfortably add child elements while creating the parent. E.g.
In the view (assuming
index.gsp
) you have something like:Having an
addQuestion
action within your controller:Editing is another topic, but works the same way.
Have a look at this blog post:
http://blog.andresteingress.com/2012/06/29/groovy-2-0-love-for-grails-command-objects