Grails Has Many Relationships

In Getting Started with Grails I went over some installation quirks in Grails to generating a scaffolding for a blog post domain model. In this entry, we will create a comments domain model class and link both domain classes so that post can have many comments. To start off, lets add some fields in your model (remember that up to now we have not defined our database schema).

Open the grails-app/domain/Post.groovy domain class, created in Getting Started with Grails, and add two string fields to represent the title and post data. Add a date to represent the created at date. The post domain class should look something like the following:

class Post {
  Long id
  Long version
  String title
  String post
  Date createAt

  // toString, equals, hashCode
}

If you start the grails application via the ‘grails run-app’ target and direct your browser to http://localhost:8080/blogjet/post/list you will be able to create, read, update, and delete posts with a title and post data.

Lets create the comment domain class by running the following target from the grails application directory.

blogjet$>grails create-domain-class

When prompted to name the domain class, enter ‘comment’ as the name. The Comment.groovy domain class will be generated in grails-app/domain. Now we need to update the Comment.groovy domain class by adding the title, comment, and adding the relationship between comments and posts. A comment belongs to a post while at the same time a post can have many comments. Edit the Comment.groovy class to look something like the following:

class Comment {
  Long id
  Long version
  String title
  String comment
  Date createAt

  // The belongs to relationship
  def belongsTo = Post
  Post post

  // toString, equals, hashCode
}

As mentioned earlier, a blog post can have many comments. To complete the relationship between posts and comments we need to update the the post domain class to include a set of comments and the ‘has many’ relationship. Add the following two lines to the post domain class.

def relatesToMany = [comments : Comment]
Set comments = new HashSet()

Unfortunately, the post scaffolding will not create the necessary forms to create a new comment for the post. We need to create a controller with scaffolding for the comment domain model. To create a new controller run the following grails target:

blogjet$>grails create-controller

When prompted to name the domain class, enter ‘comment’ as the name. A CommentController.groovy controller class will be generated in grails-app/controllers. Open this class and add the updated as follows:

class CommentController {
  def scaffold = Comment
  def index = { }
}

Now to create posts with comments direct your browser to http://localhost:8080/blogjet/post/list. Click the ‘New Post’ menu, enter the post title and post data. In the ‘Show Post’ page click the ‘Edit’ button to add a comment. In the ‘Edit Post’ page click the ‘Add Comment’ link to create a comment associated with this post. The scaffolding creates all the CRUD views and basic navigability.

Now a note about scaffolding. If you want to see what grails generates for you use the file explorer to view the contents under the grails application’s tmp/war/WEB-INF/grails-app/views/ directory.

Technorati Tags: , , , , , , , ,

Related posts:

  1. Gettings Started With Grails
  2. Holy Guacamole Web Development With Grails
  3. Rapid Web Development With Grails
  4. Acts As Commentable Plugin
  5. Grails, Sails, and Trails – Rails Through a Coffee Filter
  6. The Great Grails Switch


Leave a Reply