0

I have been using Grails addTo and removeFrom dynamic methods and they are awesome, Usually I use them in following way

String id =params.quizId
Quiz quizInstance = Quiz.get(Long.parseLong(id))// **1**
for( String q in params.questionCheckBox)
{   
    Question question = Question.get(Long.parseLong(q))//**2**
    quizInstance.addToQuestions(question)//**3**
}

Being a person who used to work on java I visualize this piece of code as following three steps

  1. run a select query to get quizInstance
  2. run a select query to get question
  3. run an insert query to insert questions in QUIZ_QUESTION table

I do see that third step only requires just quiz id and and Question id then why to perform first two steps. Is there an "addTo" kind of static method which I can use to say Quiz.addToQuestionAndQuiz(questionId,QuizId) Please correct me if I am wrong in assuming the way things will go on db side.

Also is there a way to add a list of values dirctly using addTo method or must I use a for loop?

Charles
  • 50,943
  • 13
  • 104
  • 142
Sap
  • 5,197
  • 8
  • 59
  • 101

1 Answers1

0

This is just a suggestion - I don't know if this will work correctly or not:

String id =params.quizId
List<Question> questionList = Question.findAllByIdInList(params.questionCheckBox)
Quiz quizInstance = Quiz.get(Long.parseLong(id))
quizInstance.questions += questionList

Let me know if that works for you

James Kleeh
  • 12,094
  • 5
  • 34
  • 61