I am experimenting with Grails 2.0.1 and have read through the documentation but am confused by the new dynamic methods:
def book = Book.findOrSaveWhere(author: 'Daniel Suarez', title: 'Daemon')
def book = Book.findOrSaveByAuthorAndTitle('Daniel Suarez', 'Daemon')
I can imagine some situations where
findOrCreateBy/Where might come in handy but for the life of me I can't think of any situations where
findOrSaveBy/Where would be called for. Even in the test case on github the motivation for this method isn't obvious to me. It seems like in each test case either you clearly want to
find or clearly want to
save so having a
findOrSave just makes the intent of the code less clear.
The original JIRA entry for these features doesn't discuss the motivation or give any background. Could someone please give me some scenarios where these methods would be beneficial?
findOrCreateWhere - User.findOrCreateWhere(login: ‘bala’), this will find a user in the database with login as bala. If it does not find one it will create a new object but will not save it. You have to save it explicitly.
findOrSaveWhere - User.findOrSaveWhere(login: ‘bala’), this will find a user in the database with login as bala. If it does not find one it will create a new object and also will save that.
Hope that helps.