• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't update the object in the database

 
Maharaj thak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I have written a very simple class for a Grails app ...

class AppUser{

String blocked="no"
}

And in my controller to block the user.....

def lockme={
def per=AppUser.get(session?.user?.id)
per.blocked="blocked"
try{
per.save()

println "saved user"



}catch(Exception e){
println "reason is ...${e.printStackTrace()}"


}


}



On executing the code it prints "saved user" but the changes don't show up in the database.

Could anybody help please.

Regards
Mauji Thak
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you in a transaction that needs to be committed?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're saving in a controller, you really should flush the changes...



Although, good practice is to put transactional code in a service. Also, if it is still not persisting your changes you should check the object for errors:



And actually, you should be propagating those errors back up to the user.



The above code assumes a lot. You should adjust for your actual code and use case.
 
Maharaj thak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg and Jeanne
I think the when I was doing

def per=AppUser.get(session?.user?.id) it was getting the object from the session.what i did was



per.refresh() //to get the copy from the database and not session
per.blocked="blocked"
per.save()


and it worked.

Regards
Mauji thak
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's good that it worked. I would still recommend reading what I wrote and implementing the service part of it. If you don't, you will have problems down the road.
 
Maharaj thak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Found out what the real problem is.But first of all here's my domain class

class AppUser{

String name
String password


static constraints={
password(blank:false,size:6..20)
}


def beforeInsert = {
password = password.encodeIt()
println "password length is ${password.size()}"
}




}

Now if I try and save a user I don't get any errors(even though the beforeInsert event prints "password length is 60" which is more than what i specified in contraints).If I try and update the user and call save() on it I get errors...and the errors are all related to the size of the password.Increasing the size of the password constraint from 20 to anything more than 60 solves it.I don't know why but it works.


Regards
Mauji Thak

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic