Search code examples
javagrailsgrails-orm

Exclude 'id' from being retrieved from query in Grails


I have to display a list of items with their details. The controller looks like this

def showItems() {
    def items = Item.list(offset:0, max:10, sort:"updatedOn", order:"desc")
    render view : "show", model : [items : items]
}

This works perfectly fine but the problem is that the 'id' of the items is also being sent to the gsp which I don't want. How can I send all the item details from the controller to the gsp except the 'id'.


Solution

  • I don't know why you care about the ID being sent to the view, but, you can do something like:

    Item.list().collect { [prop1: it[prop1], ...] } 
    

    to send only the properties you want.

    Another option:

    Item.list().collect { it.subMap('key1', 'key2') }
    

    And even more Groovy:

    Item.list().collect{ 
        def keys = it.keySet()
        keys.remove('id')
        it.subMap( keys ) 
    }​​