From 443f1de14643dc4624de92cbdb5253368659a139 Mon Sep 17 00:00:00 2001 From: Yang Qing Date: Fri, 5 Jul 2019 16:11:35 +0800 Subject: [PATCH] The code block should be limited in ("has_many", "has_one") to avoid wrong settings of other relation kinds --- association.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/association.go b/association.go index a73344fe..967ad9f0 100644 --- a/association.go +++ b/association.go @@ -276,6 +276,12 @@ func (association *Association) Count() int { fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)), toQueryValues(primaryKeys)..., ) + if relationship.PolymorphicType != "" { + query = query.Where( + fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)), + relationship.PolymorphicValue, + ) + } case "belongs_to": primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value) query = query.Where( @@ -284,13 +290,6 @@ func (association *Association) Count() int { ) } - if relationship.PolymorphicType != "" { - query = query.Where( - fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)), - relationship.PolymorphicValue, - ) - } - if err := query.Model(fieldValue).Count(&count).Error; err != nil { association.Error = err }