Search code examples
grailsgrails-orm

Accessing a value in beforeInsert event of Grails


Is there a way to access value inside the beforeInsert event?

For example:

Controller:

def save () {
   user = new User(params)
}

Domain:

String name
String job
String name_and_job

beforeInsert {
  name_and_job = name+job
}

In the above beforeInsert event I want to get the current name and job and add those to name_and_job. However, this doesn't work.

Please note that this is just an example of the bigger problem I'm working with. Should this type of stuff be done in the controller instead?


Solution

  • The controller should be like:

    def save () {
       user = new User(params).save()
    }
    

    Without the save() call, you just instantiated a transient User object. That's why your beforeInsert event never got activated.