Reputation: 1673
it is very common to use domainClass.properties = params to bind all incoming request params to a domain class object. i do not understand why this assignment automatically updates the domain object. e.g.
def update = {
def book = Book.get(1)
book.properties = params
book.discard()
}
the params assignment updates the domain object. discard method has no effect. can anyone help me?
Upvotes: 0
Views: 476
Reputation: 482
I don't know why the answer by Ruben has been selected as correct. @hitt5 the behavior is as expected but yes it's different and you will find the reason here: http://blog.springsource.org/2010/06/23/gorm-gotchas-part-1/ See the section 'Now you're saving when I don't want you to?!' in particular.
Upvotes: 0
Reputation: 9120
The discard() method will not reset the book instance, only prevent that it will be saved automatically by Grails. To reset the book instance you need to reload it, e.g. using Book.get(1).
And book.properties = params will simply try to assign the elements in the params map to properties of Book where the property name is the same as the map entry key. I believe this is standard Groovy functionality, and not Grails specific.
Upvotes: 2