0

I have the following domain class:

class StudentQuiz {
      Date dateCreated
      Date lastUpdated
      Quiz quiz
      float price
      Student student
      Date startTime
      Date endTime
      int score
      List answers
      static hasMany = [answers:Answer]
      static constraints = {
         answers nullable:true
      }

}

But when I use the following code to save an instance of this class:

 studentInstance =  (Student)User.findByEmailAndPassword(params.email, params.password.toString().encodeAsPassword())
 if (studentInstance) {
    StudentQuiz studentQuizInstance = new StudentQuiz(score:0,  quiz:quizInstance,price:quizInstance.price,student:studentInstance,startTime:new Date())
    if (!studentQuizInstance.save(flush:true)) {                    
      studentInstance.errors.each {
            println "===="+it+"-------"
        }
}

I am getting this:

grails.validation.ValidationErrors: 0 errors

with no other explanation of the error. Any help would be really appreciated.

Ben Klein
  • 1,719
  • 3
  • 18
  • 41
Sap
  • 5,197
  • 8
  • 59
  • 101

1 Answers1

0

you are checking the wrong instance. it should be as follows:

if (!studentQuizInstance.save(flush:true)) {                    
    studentQuizInstance.errors.each {
    println "===="+it+"-------"
}
hitty5
  • 1,653
  • 12
  • 25
  • Question is, why studentQuizInstance is not saving, I already have the errors printed for it in the given code – Sap Jan 10 '13 at 14:11
  • you do not get error messages because in your code the domain class gets never validated. have you tried to use the hasError method before saving()? Maybe your parameter list doest not match your doman class constraint. – hitty5 Jan 10 '13 at 14:24
  • wouldn't !studentQuizInstance.save(flush:true) condition do the same thing? and studentInstance.errors.each { println "===="+it+"-------" } would print the errors? – Sap Jan 10 '13 at 14:35
  • Yes, you are right! See http://grails.org/doc/latest/ref/Domain%20Classes/save.html. "The save method returns null if validation failed and the instance was not persisted, or the instance itself if successful". When the validation fails during the save call, the errors are available in the errors object. You try to print the errors of studentInstance, but i should be studentQuizInstance. Thats the solution! – hitty5 Jan 10 '13 at 15:16