Filter 'belongsTo' results

144 views Asked by At

Im new in grails and its being a tough job try to understand it without someone who can ask this types of questions. I have the following problem. This are two of my domain classes I have: Pagadora and Calculo

Pagadora.groovy

class Pagadora {
     String nombre
     Boolean activo
     static hasMany = [calculoIMSS: Calculo, calculoPagado: Calculo, calculoConvenio: Calculo]
     static mappedBy = [calculoIMSS: 'imss', calculoPagado:'pagadoPor', calculoConvenio:'convenio']
}

the Boolean variable activo refers if the "Pagadora" will gonna show up in the "select list" of the view in the class Calculo

Calculo.groovy

class Calculo {
    String nombre
    BigDecimal sueldo
    static belongsTo = [imss:Pagadora, pagadoPor:Pagadora, convenio:Pagadora]
}

What I want is the following, i want to filter in the select, only the Pagadoras that are active

 <g:select id="imss" name="imss.id" from="${Pagadora.Pagadora.list(sort: "nombre", order: "asc")}" optionKey="id" required="" value="${calculoInstance?.imss?.id}" class="many-to-one form-control" noSelection="['':'-Escoja la empresa-']" />    

Sorry If it was a little hard to understand me, but it was a little difficult for me try to explain something like this in english (my native language is Spanish :P)

1

There are 1 answers

3
Nathan Hughes On BEST ANSWER

If you want to get all the entries that are active, change the from attribute to:

from="${Pagadora.findAllByActivo(true) [sort: 'nombre', order: 'asc']}"

This is a dynamic finder.

Jeff points out in the comments that the boolean parameter here can be omitted (see examples for findAllBy). If you want to get all the inactive entries, you could still do without the parameter by naming your finder findAllByNotActivo. This comes in especially handy when you have two parameters for your finder where one is a boolean.