Say I have these two domain classes ....
class Design{
static hasMany=[contributors:Contributor]
}
class Contributor{
static hasMany=[designs:Design]
static belongsTo=Design
}
I add contributors to designs as time passes using
def design=Design.get(params.designId)
...
design.addToContributors(newContributor).
Suppose I want to keep track of the date that a relationship was created between a contributor and a design. Without the grails gorm abstraction I would add a date column to the relationship table
design_rel_contributor
| id | design_id | contributor_id | date_created |
But I do not know how to implement such "metadata" for many-to-many relationships in grails.
You can simply add the dateCreated
column and then populate it by implementing beforeInsert()
:
class Contributor {
Date dateCreated
static hasMany = [designs:Design]
static belongsTo = Design
static constraints = {
dateCreated nullable: true
}
void beforeInsert() {
dateCreated = new Date()
}
}