1

I am patching a domain object in my code. At the end, I need to save the object but only if it has been actually changed. Is it possible to avoid custom boolean flags having code like that?

User user = User.find(...)
if(maybe)
  user.name = "John"
if(user.changed())
  user.save()
4

1 回答 1

3

您可以使用isDirty进行此检查。

if(user.isDirty() && user.save()) {
  // user saved successfully
}
于 2012-11-20T10:35:08.827 回答