How to parse, persist and retrieve a string with tags separated by spaces?

grails, groovy, tags, tokenize

Solution

Roughly, something like this:

class Post {
    static hasMany [tags:Tag]
}

class Tag {
    static belongsTo = Post
    static hasMany [posts:Post]
}

class someService {

    def createPostWithTags(name, desc, tags) {      
        def post = new Post(name: name, desc: desc).save()
        tags.split(' ').each { tagName ->
            def tag = Tag.findByName(tag) ?: new Tag(name: tagName)
            post.addToTags(tag).save()
        }       
    }

}

Problem

My database consists of 3 tables (one for storing all items, one for the tags, and one for the relation between the two): Table: Post Columns: PostID, Name, Desc Table: Tag Columns: TagID, Name Table: PostTag Columns: PostID, TagID What is the best way to save a space separated string (e.g. "smart funny wonderful") into the 3 database tables shown above? Ultimately I would also need to retrieve the tags and display it as a string again. Thanks!

Original source