문제

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()
도움이 되었습니까?

해결책

You can use isDirty to make this check.

if(user.isDirty() && user.save()) {
  // user saved successfully
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top