user2782001
user2782001

Reputation: 3488

grails gorm way to store the date a many-to-many relationshp was created

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.

Upvotes: 1

Views: 60

Answers (1)

helgew
helgew

Reputation: 361

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()
    }
}

Upvotes: 2

Related Questions