Badmiral
Badmiral

Reputation: 1589

saving a second version of a grails object

one thing I would like to do is version the entries in our database. In my update method I would like to save a newer second version of an object. For instance

def update = {
    def VariantInstance = Variant.get(params.id)
    def NewVariantInstance = VariantInstance
    NewVariantInstance.properties = params
    if (VariantInstance) {
        if (!VariantInstance.hasErrors()) {
            VariantInstance.save()
            NewVariantInstance.save()

            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'Variant.uniqueIdentifyingName', default: 'Variant'), VariantInstance.id])}"
            redirect(action: "list")
        }
        else {
            render(view: "edit", model: [VariantInstance: VariantInstance])
        }
    }
    else {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'Variant.uniqueIdentifyingName', default: 'Variant'), params.id])}"
        redirect(action: "list")
    }
}

While this saves the current one, it does not create a new one. What am I doing wrong?

Upvotes: 0

Views: 63

Answers (1)

Aquatoad
Aquatoad

Reputation: 788

There's two problems; NewVariantInstance is going to just be a reference to the VariantInstance most likely, so it's dereferencing the same object. Additionally, when you do your params assignment you're also assigning the id field from VariantInstance to NewVariantInstance, so GORM will see the objects as the same when it does the save.

Upvotes: 1

Related Questions