GORM - add a child to the parent and save the groovy.lang.MissingMethodException command

This is my domain model, the survey has many questions, and each question has many repsons:

class Survey { String name String customerName static hasMany = [questions: SurveyQuestion] static constraints = { } } class SurveyQuestion { String question static hasMany = [responses : SurveyQuestionResponse] static belongsTo = [survey: Survey] static constraints = { } } class SurveyQuestionResponse { String description static belongsTo = [question: SurveyQuestion] static constraints = { } } 

In my controller, I have a method that takes the identifier for the survey, looks at it, then builds the question from another query parameter, tries to add the question to the poll and save it:

 def addQuestion = { def question = new SurveyQuestion(question:params.question) def theSurvey = Survey.get(params.id) theSurvey.addToQuestions(question) //fails on this line theSurvey.save(flush:true) redirect(action: showSurvey, params:[id:theSurvey.id]) } 

However, it fails and returns this:

No method signature: roosearch.Survey.addToQuestions () is applicable for argument types: (roosearch.SurveyQuestion) values: [roosearch.SurveyQuestion: null] Possible solutions: addToQuestions (java.lang.Object), getQuestions ()

I don’t quite understand what I'm doing wrong here, I tried various alternative ways to create a question, even creating an instance of one manually with a literal string, but it always gives the same error.

Can anyone advise me?

thanks

+6
source share
3 answers

The problem is that you do not have a “question”, so it is not in the database yet. Try to save the “question” first and then add it to the survey. Something like that:

 def addQuestion = { def question = new SurveyQuestion(question:params.question).save() def theSurvey = Survey.get(params.id) theSurvey.addToQuestions(question) theSurvey.save(flush:true) redirect(action: showSurvey, params:[id:theSurvey.id]) } 
+1
source

(I do not have enough comments for comments, so I will answer).

Firstly, it looks OK.

I learned to accept error messages with my face value . For some reason, he thinks the “question” is zero. I suggest that you can insert some entries and see that this is not the case.

At this point, I will first try to save the question, see that it correctly saves and receives the assignment, and id, and then calls addToQuestions.

0
source

can you try to state whether SurveyQuestion was created using input parameters? eg

 assert question 

right after the line

 def question = new SurveyQuestion(question:params.question) 

and as #alcoholiday suggested, try some entries as well. or simple

 println params 

can give you a quick look

0
source

Source: https://habr.com/ru/post/921614/


All Articles