Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 229
Jimmy Avatar asked Jul 29 '12 16:07

Jimmy


1 Answers

The problem is that you dont have "question" saved so it is not in the database yet. Try to save first the "question" and then add it to the survey. Something like this:

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])
    }
like image 180
David Chavez Avatar answered Oct 02 '22 00:10

David Chavez