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?
Well you can get the
sender
first:Then pass it as criteria to your
where
closure:You get Something like this: