Q:

Having one class in one of two one-to-many relationships (eg: Comments on Video OR Image) in Grails?

Im building an example where there exists Video and Image domain models. Each have a one-to-many relationship with the Comment model as follows:
package commentstest

class Video {

    static constraints = {
        embeddUrl(blank:false,nullable:false,url:true)
    }

    String embeddUrl

    static hasMany = [comments:Comment]
}

and
package commentstest

class Image {

    static constraints = {
        fileName(blank:false,nullable:false)
    }

    String fileName

    static hasMany = [comments: Comment]
}

and finally the Comment class
package commentstest

class Comment {

    static constraints = {
        body(blank:false,nullable:false)
    }

    String body

    static belongsTo = [image:Image, video:Video]
}

Now the problem i'm having is that when i create a 'comment' it must be able to be added to either a video OR an image. Currently, the scaffolding populates both the image and video option, and doesnt give the option to leave empty for one of those fields.
Anyone have an idea how to do this? Im sure my problems are spawning from the line:
static belongsTo = [image:Image,video:Video]

But i dont know how to specify that it must belong to one OR the other. not both.

A:

From what I could see and understand, it's like you saystatic belongsTo = [image:Image,video:Video] is the root cause.
What you should do is add something like this to your constraints in Comment
   static constraints = {
        fileName(blank:false,nullable:false)
        image(nullable:true)
        video(nullable:true)
    }

That enables you to set it to either a video or image.
Hope it hepls!

