views:

267

answers:

2

Hi

I have the following domain classes:

class User = {
       String username
       ... 
       Company company
 }

class Company {
    String name
    ...
}

That is, there is a n:1 relationship between user and company. These classes are so and I cannot change them.

At the show.gsp I want to have the details of the company together with links to the users who belongs to this company.

I know that I can achieve this writing an own tag, but I am sure that this would be possible using the each tag or the the findAll tag.

If I do the following

<g:each in="${User.findAll('from User order by username')}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

I've tried to pass the ${companyInstance} as a parameter but either I got an exception or it didn't work.

I've also tried using User.findAllByCompany.

When using:

<g:findAll in="${user}" expr="it.company == ${companyInstance}  ">

I am getting an empty set.

Is there an easy way to achieve this without writing a taglib?

Thanks in advance.

Luis

A: 

It's more simple that you think: ${userInstance.company.encodeAsHTML()}

Aaron Digulla
I think I was ot clear enough. I am at company view. I mean, I don't have the userInstance. I have the companyInstance.
Luixv
A: 

The <g:findAll> tag iterates over an object list but it does not query the DB as done with User.findAll(..)

The correct code is:

<g:each in="${User.findAll('from User as u where u.company=:company order by username', [company: companyInstance])}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

If you absolutely want to use g:findAll, then you first need to build the list of users as following:

<% def users = User.listOrderByUsername()%>
<g:findAll in="${users}" expr="it.company == ${companyInstance}  "> 

I hope it helps,

Regards,

Fabien.

fabien7474
Thanks a lot! it works perfectly.
Luixv