From 6b7d18656d8af6565ea831830f06309c3f8c9c12 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 25 Feb 2021 20:06:26 +0800 Subject: [PATCH] Lazy call registered scopes --- chainable_api.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/chainable_api.go b/chainable_api.go index 58b9336f..5415f5bd 100644 --- a/chainable_api.go +++ b/chainable_api.go @@ -240,11 +240,10 @@ func (db *DB) Offset(offset int) (tx *DB) { // } // // db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders) -func (db *DB) Scopes(funcs ...func(*DB) *DB) *DB { - for _, f := range funcs { - db = f(db) - } - return db +func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) { + tx = db.getInstance() + tx.Statement.scopes = append(tx.Statement.scopes, funcs...) + return tx } // Preload preload associations with given conditions