Manuel Calles
Manuel Calles

Reputation: 185

Grails: How to define a domain property rendered on views but not persisted?

I have this domain classes, let's say:

class Person {
  String name
  Integer age
  //car data that needs to be shown and filled in views 
  //but not persisted in Person class
  String model
  String color

  static afterInsert = {
    def car = new Car(model: model, color: color)
    car.save()
  }
}

class Car {
  String model
  String color  
}

What I need is to show in my Person views (create and edit) the model and color properties that are defined inside Person class but these doesn't have to be persisted with this class. These data, model and color, have to be persisted using the Car domain class maybe using the afterInsert event. In other words, I need to save data from a domain class using the views from another domain class.

Thanks in advance.

Upvotes: 0

Views: 235

Answers (1)

ikumen
ikumen

Reputation: 11643

You can use transients on properties you want GORM to ignore, for example

class Person {

  static transients = ['model', 'color']

  String name
  Integer age
  //car data that needs to be shown and filled in views 
  //but not persisted in Person class
  String model
  String color
  ..
}

Just curious but is there a reason you're not using associations

class Person {
  ..
  static hasMany = [cars: Car]
}

class Car {
  ..
  static belongsTo = [Person] 
  static hasMany = [drivers: Person]
}

.. or composition

class Person {
  Car car
}

or simply data binding with multiple domains

//params passed to controller
/personCarController/save?person.name=John&age=30&car.model=honda&car.color=red

//in your controller
def person = new Person(params.person)
def car = new Car(params.car)

Upvotes: 1

Related Questions