tags:

views:

457

answers:

1

I'm learning grails, and I have a problem.

I have 2 classes, lets say:

    class Book {

      String name
      String description
      static belongsTo = Category
      Category category

      static constraints = {
           name(nullable:false, blank:false)
               description(nullable:true, maxSize:5000)
      }

   class Category {

      String name
      static hasMany = [books:Book]
      Set books
      static constraints = {
              name(nullable:false, blank:false)
      }

      String toString(){
              this.name
      }
   }

When I create a book I want to see a drop down list with all the categories name. If I don't select a category than I shouldn't be able to create that book. If there are no categories in the database than I shouldn't be able to create a book, until I create and select a category.

Is possible for this features to be generated (with grails generate-all) from the domain classes if I have the right constraints and fields? If yes, how?

+3  A: 

I could be off here, but I think the format of your belongsTo statement is incorrect. In the examples I've seen and classes I've used it's been:

static belongsTo = [category:Category]

Then I believe you also need to have the Category set to nullable:false

static constraints = {
           name(nullable:false, blank:false)
           description(nullable:true, maxSize:5000)
           category(nullable:false)
      }

This should at least generate the dropdown on the view and disallow creating a Book without a Category.

Shaun
It works, thank you!It was so obvious, foolish of me to miss it.
Andrei T. Ursan