Zaccak Solutions
Zaccak Solutions

Reputation: 249

Grails doesn't save when using findAll but does when using collection

I have this code:

void updateIndex() {
    Visit.withNewSession {
        def results = patient.visits.sort{ it.date }

        results.eachWithIndex { item, index ->
            item.index = index
        }
    }
}

This saves to the DB without a save(). I changed the results to use a findAll query (because I only wanted a selection of them) and now it doesn't save the changes to the DB. I do see the changes to the indices but these changes never get persisted.

def results = Visit.findAllByPatientAndTypeInList(
                this.patient,
                [Type.Test, Type.Junk]
            ).sort{ it.date }

Is this one of those GORM gotcha's? I tried to explicit call a save (and check for any errors) and still no luck.

Upvotes: 0

Views: 239

Answers (1)

injecteer
injecteer

Reputation: 20699

Those are 2 different pairs of shoes.

In the 1st case you were sorting a list of hasMany objects, which are linked to the patient via a join-table with an list index field. So when you sort the list, the join-table's index field gets dirty and will be updated upon tx-commit.

In the 2nd case, you get a list of instances but no list index info. So, whatever you do to the list - re-ordering, adding or removing its entries - does NOT make it's instances dirty. Ergo, they are not saved even upon save()

Upvotes: 3

Related Questions