Skip to content

Instantly share code, notes, and snippets.

@pabloalba
Created October 14, 2013 15:54
Show Gist options
  • Save pabloalba/6977872 to your computer and use it in GitHub Desktop.
Save pabloalba/6977872 to your computer and use it in GitHub Desktop.
Get a list of ids, and show the elements. This code is too damn long for this simple task. Improvements?
def showContents(){
def contents = []
def ids = params.list('id').findAll { it.isLong() }*.toLong()
if (ids) {
contents = Content.createCriteria().list {
'in' 'id', ids
}
}
render view:'showContents', model:[contents:contents]
}
@ilopmar
Copy link

ilopmar commented Oct 15, 2013

En lugar de hacer el criteria puedes poner:

...
if (ids) {
   contents = Content.getAll(ids)
}

Otra mejor sería comprobar qué ocurre si la lista de ids es vacía, por lo que a lo mejor te podrías ahorrar el if y demás y dejar el código así:

def showContents(){
    def ids = params.list('id').findAll { it.isLong() }*.toLong()
    def contents = Content.getAll(ids)
    render view:'showContents', model:[contents:contents]
}

@ilopmar
Copy link

ilopmar commented Oct 15, 2013

Ohhhhh, acabo de hacer la prueba y no funciona (al menos en 2.1.5)

@Alotor
Copy link

Alotor commented Oct 15, 2013

Si no funciona el metodo "getAll" puedes usar "findAllByIdInList" que debería comportarse igual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment