From 0fd2fd66d539b47e89b60afcf9f1799252da307b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 9 May 2016 22:42:07 +0800 Subject: [PATCH] Refactor handleHasManyPreload --- callback_query_preload.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/callback_query_preload.go b/callback_query_preload.go index d95eae07..a4b1cf1f 100644 --- a/callback_query_preload.go +++ b/callback_query_preload.go @@ -160,15 +160,10 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{}) preloadMap[toString(foreignValues)] = append(preloadMap[toString(foreignValues)], result) } - objects := scope.IndirectValue() - for j := 0; j < objects.Len(); j++ { - object := reflect.Indirect(objects.Index(j)) + for j := 0; j < indirectScopeValue.Len(); j++ { + object := indirect(indirectScopeValue.Index(j)) objectRealValue := getValueFromFields(object, relation.AssociationForeignFieldNames) - objectStringValue := toString(objectRealValue) - if results, ok := preloadMap[objectStringValue]; ok { - if object.Kind() == reflect.Ptr { - object = object.Elem() - } + if results, ok := preloadMap[toString(objectRealValue)]; ok { f := object.FieldByName(field.Name) f.Set(reflect.Append(f, results...)) }