Adds a domain class relationship for one-to-many or many-to-many relationship, where the relationship is indicated by the property used as the suffix to the method.
def fictBook = new Book(title:"IT") def nonFictBook = new Book(title:"On Writing: A Memoir of the Craft") def a = new Author(name:"Stephen King") .addToFiction(fictBook) // 'fiction' is name of relationship property .addToNonFiction(nonFictBook) // 'nonFiction' is name of relationship property .save()
The addTo*
method is a dynamic method that uses an association to automatically add instances to the association. It also automatically configures bi-directional relationship so that both sides o the relationship have been set.
The method expects an instance of the associated class:
def fictBook = new Book(title:"IT") def nonFictBook = new Book(title:"On Writing: A Memoir of the Craft") def a = new Author(name:"Stephen King") .addToFiction(fictBook) // 'fiction' is name of relationship property .addToNonFiction(nonFictBook) // 'nonFiction' is name of relationship property .save()
Or a map, in which case it auto-instantiates an instance of the associated class:
def a = new Author(name:"Stephen King") .addToFiction(title:"IT") .addToNonFiction(title:"On Writing: A Memoir of the Craft") .save()