zoran119
zoran119

Reputation: 11307

How to add a property to a map?

I have a domain class:

class Person {
    String name
    Boolean likesGrails
    Boolean isSmart
}

and want to pre process the data (create a new property friend) before passing it to a view (which will use friend to decide stuff):

def people = Person.list()
people.each {
    it.friend = likesGrails && isSmart
}

How do i add this friend property? The code above doesn't work (it complains that it.friend doesn't exist).

Upvotes: 1

Views: 241

Answers (2)

dhore
dhore

Reputation: 83

You can add a transient if you don't want it to be stored in your database.

class Person {
       String name
       Boolean likesGrails
       Boolean isSmart

       Boolean friend
       static transients = [ 'friend' ]
}

but you can't add random properties on the fly to domain classes.

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

You should just be able to add:

static transients = [ 'friend' ]
public boolean isFriend() {
  likesGrails && isSmart
}

To your domain class, then access person.friend in your view

Upvotes: 6

Related Questions