GORM - add child onto parent and save is giving groovy.lang.MissingMethodException

Viewed 2224

This is my domain model, a survey has many questions, and each question has many repsonses :

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 in the ID for a Survey, looks it up, then builds a question from another request parameter, tries to add the question to the survey 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 signature of method: roosearch.Survey.addToQuestions() is applicable for argument types: (roosearch.SurveyQuestion) values: [roosearch.SurveyQuestion : null] Possible solutions: addToQuestions(java.lang.Object), getQuestions()

I'm not quite understanding what I'm doing wrong here, I've tried various alternative ways to create the question, even instantiating one manually with a literal string, but it always gives the same error.

Can anyone please advise me?

Thanks

3 Answers
Related