tags:

views:

94

answers:

2

Hi, I have a class :

class Category {
  String name
  SortedSet items
  static hasMany = [items:Item]
}

Inside the controller, I render category as XML (converters) :

   def getCategory = {
     render Category.read(1) as XML
   }

But I want exclude items from the rendering.

How can I do ?

Thanks

A: 

You need to create a second domain class sort of like a view and configure the mapping so it has the same table as the Category class

See the entire answer on this thread : http://stackoverflow.com/questions/2789176/how-to-restrict-visibility-of-domain-properties-in-grails/

Fabien.

fabien7474
why not. But It's more elegant if I can configure a collection of xml view for my object. Your solution is a trick but is not really object oriented.Why does not create a wrapper :XMLView(object){ static view1 = { object.params1 object.params2 } static view2 = { object.params2 object.params3 } }May be can I create an issue on the jira codehaus for this ?Thanks a lot.
I created a jira issue (wish) :http://jira.codehaus.org/browse/GRAILS-6269
+1  A: 

You can simply return a Map with only the properties you want to include:

def getCategory = {
    def category = Category.read(1)
    render [ id: category.id, name: category.name ] as XML
}
Vladimir Grichina
It's not very elegant. Because if I have a book collection, I must iterate.Book.list().each{ ... }
But it's the better solution. Thx
You should use collect() method, not each().For example:Book.list().collect{ book -> [ name: book.name ] } as XML
Vladimir Grichina