Sagarmichael
Sagarmichael

Reputation: 1624

access through domains

I am using grails 2.0.1. I create a user and under this user I create a child. Each child will then have their own page.

How would I access the details in the child domain from the profile page?

User domain as follows:

String username
String password
String email

static hasMany = [children:Child, followed:User]

Child domain as follows:

String firstname
String otherNames
String lastname
Date dateOfBirth

I think I need to add a

static belongsTo = User

in the Child domain. But I am not sure how I can access this in a controller. As a user can have many children and I need to know which one has been selected.

Also can you add a variable to be passed when using a g:link tag?

Upvotes: 0

Views: 35

Answers (1)

Igor
Igor

Reputation: 33993

I am not sure what the profile page is, I'm assuming it is a page to view the User properties. Please post the code if this is not the case.

To access the children of the User, you can simply refer to yourUser.children. This simply returns a Collection, so you can iterate over it, grab the first one, etc. If you need a specific child that was selected from a gsp, you will need to pass its id (or code, or some other unique identifier) to your controller action, and find it from your User. I'm assuming that's what you mean by your second question.

As for the g:link, yes you can pass a variable, like so:

<g:link action="yourAction" params="[childId: childId]">

Upvotes: 1

Related Questions