I'm trying to put together a basic blog application using Grails 2.4.4. My domain model is as follows:
class Commentable {
String title
static hasMany = [comments:Comment]
}
class Comment extends Commentable {
static belongsTo = [target:Commentable]
}
class Post extends Commentable {
static hasMany = [tags:Tag]
}
class Tag {
String label
static hasMany = [posts:Post]
static belongsTo = Post
}
In the init method of BootStrap.groovy, I'm trying to create a Post and a Tag as follows
def post = new Post();
post.setTitle("Post1");
post.save();
def tag = new Tag();
tag.setLabel("Tag1");
tag.save();
tag.addToPost(post);
tag.save();
which produces the error message below:
Message: No signature of method: io.dimitris.blog.Tag.addToPost() is
applicable for argument types: (io.dimitris.blog.Post) values:
[io.dimitris.blog.Commentable : 1]
Possible solutions: addToPosts(java.lang.Object)
Any hints on what I'm doing wrong would be really appreciated.
You are calling tag.addToPost(post)
but you need tag.addToPosts(post)
. The hasMany
property is static hasMany = [posts:Post]
. The key in that map dictates the method name. If you change that to static hasMany = [post:Post]
then the method would be addToPost(post)
, but the name would make less sense.