How to reference implicit parameters in a where closure

68 Views Asked by At

How can I use an implicit parameter inside a where closure?

I'm not especially proud of my current workaround:

def index() {
    def params = params                 // <-- UGLY HACK
    respond Project.where {
        if (params.sender) {
            sender.id == params.sender  // <-- OTHERWISE THIS WOULD FAIL
        }
    }
}

Also I noticed that I can parameterize the query by putting generic Groovy code inside the closure, such as the if above. Strangely, the params inside the conditional did not cause any trouble, even without the hack. Is this practice OK or is it discouraged?

1

There are 1 best solutions below

0
On

Well you can get the sender first:

def thesender = Sender.get(params.sender)

Then pass it as criteria to your where closure:

Project.where { sender == thesender }

You get Something like this:

def index() {
    def thesender = Sender.get(params.sender)

    respond Project.where { sender == thesender }
}