Skip to content

Instantly share code, notes, and snippets.

@jinzhu
Created September 29, 2015 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinzhu/9bcb49a05d31d9860d33 to your computer and use it in GitHub Desktop.
Save jinzhu/9bcb49a05d31d9860d33 to your computer and use it in GitHub Desktop.
reset sorting positions
package main
import (
"reflect"
"github.com/qor/qor-example/app/models"
"github.com/jinzhu/gorm"
"github.com/qor/qor-example/db"
)
func Resort(db *gorm.DB, results interface{}) {
db.Find(results)
reflectValue := reflect.Indirect(reflect.ValueOf(results))
for i := 0; i < reflectValue.Len(); i++ {
db.Model(reflectValue.Index(i).Addr().Interface()).UpdateColumn("position", i+1)
}
}
func main() {
DB := db.DB.Set("l10n:mode", "locale")
for _, locale := range []string{"en-US", "zh-CN"} {
var categories []models.Category
Resort(DB.Set("l10n:locale", locale), &categories)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment