|
Hello,
I was under the impression that this would probably work: Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save() assert comp.id I was under the impression that this would definitely work if you call flush: Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save(flush:true) assert comp.id Either way we run it the code blows up on the assert. We tried this from within a non-transactional service and a controller.
Is there something I'm missing here? Thanks, Daniel |
|
That should work unless there's a validation error. Are you sure the object saved? Check comp.hasErrors() and you can also use save(failOnError: true).
Burt > Hello, > > I was under the impression that this would probably work: > > Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the > other thing") > comp.save() > assert comp.id > > I was under the impression that this would definitely work if you call > flush: > > Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the > other thing") > comp.save(flush:true) > assert comp.id > > Either way we run it the code blows up on the assert. We tried this from > within a non-transactional service and a controller. > Is there something I'm missing here? > > Thanks, > Daniel > |
|
In reply to this post by Daniel Glauser
yup
either make sure save is returning true (you didn't get validation errors) or set grails.gorm.failOnError=true in Config.groovy On Aug 25, 2010, at 4:30 PM, Daniel Glauser wrote: Hello, |
|
Hi Josh,
Yeah, you pretty much nailed it on the head. We were calling validate ahead of time thinking that would catch any errors. We were mistaken. Bad code: void validateAndSave(boolean flushStatus) { if (this.validate()) { this.save(flush:flushStatus) } else { println("Error saving " + this)
println this.errors } } Good code: void validateAndSave(boolean flushStatus) { def savedObj def valid = this.validate()
if (valid) { savedObj = this.save(flush:flushStatus) } if(!valid || !savedObj) { println("Error saving " + this)
println this.errors } savedObj } Thanks for your help, Daniel
On Wed, Aug 25, 2010 at 4:03 PM, basejump <[hidden email]> wrote:
|
|
And in the ever so popular closure format (and minus the return type bug):
def validateAndSave = { flushStatus -> def savedObj def valid = this.validate() if (valid) { savedObj = this.save(flush:flushStatus) } if(!valid || !savedObj) { println("Error saving " + this)
println this.errors } savedObj } Cheers, Daniel |
| Powered by Nabble | Edit this page |
