tags:

views:

70

answers:

2

I want to use the lastUpdated field of a Grails domain to implement a "what objects have recently changed" view. However my domain has a has-many association which when something is added will cause the lastUpdated value to be updated.

E.g.:

class Author {
    string name
    Date lastUpdated
    static hasMany = [ books : Book ]
}

Adding a new book (author.addToBooks(newBook)) will update lastUpdated.

Questions:

  1. Why is lastUpdated not updated when a book is removed?
  2. Is there a way to prevent lastUpdated to be changed when a book is added to the list? (Let's say that a new book does not count as a change to the author in my domain model)

Thanks!

A: 

Number 1 sounds like a bug (if it's updated when you add a book it should be consistent and update it when you remove one).

Number 2 - instead of using addToBooks, have you tried new Book(author:a).save() ? You might have to refresh the Author if you need to list their books in the same action but it should stop an update firing on the author

leebutts
A: 

To get finer grained control over the date changes, you can add a "beforeUpdate" event to your domain class (see grails.org/GORM+-+Events). It looks like this is intended behavior though: http://jira.codehaus.org/browse/GRAILS-1857

Matt Lachman