newbie to grails
newbie to grails

Reputation: 42

View grails domain class property in another domain class

Hi I am just trying out grails and trying to learn more on domain class. I have two simple domain classes:

Domain Class 1

package grailtest

class Person {

    String  firstName
    String  lastName
    int age
    String  email

    static constraints = {
    }

}

Domain Class 2

package grailtest

class Customer {

    String  customerId
    Person  personInCharge

    static constraints = {
    }

}

When I do a run-app, I can only see

grailtest.Person : 1

as the Person. How can I default it to a particular value, for instance firstName + lastName, to make the application more user friendly?

Upvotes: 2

Views: 215

Answers (4)

Hubert
Hubert

Reputation: 169

You put this code in the view .gsp

${personInstance?.firstname} ${personInstance?.lastname}

Upvotes: 1

dmahapatro
dmahapatro

Reputation: 50245

You can use @ToString in case you want an elaborative way of logging or printing in standard out.

import groovy.transform.ToString

@ToString(includeNames=true, includeFields=true)
class Person {
    String  firstName
    String  lastName
    int age
    String  email
}

For example,

def person = new Person(firstName: 'Test', lastName: 'hola', 
                          age: 10, email: '[email protected]')

would give

Person(firstName:Test, lastName:hola, age:10, email:[email protected])

Upvotes: 2

Armaiti
Armaiti

Reputation: 766

Find the view where it displays grailstest.Person: 1 and update it to:

${personInstance.firstName} ${personInstance.lastName}

By default this view should be in "views/person"

Upvotes: 1

Karthick AK
Karthick AK

Reputation: 208

in the domain override toString method to what you wanted to be display. restart the app

Upvotes: 2

Related Questions