ドメインの 1 つのビューを表示しようとすると問題が発生します。いくつかの数量を合計する方法を作成しましたが、このエラーがあり、修正方法がわかりません。変数の賞が存在しないことは理解していますが、私のドメインにはこの値があります。エラーが発生するコントローラー内の唯一のメソッドのコードを次に示します。
URI /Rewards/customer/show/1
Class groovy.lang.MissingPropertyException
Message No such property: awards for class: rewards.Customer
Around line 14 of grails-app/services/rewards/CalculationsService.groovy
11:
12: def getTotalPoints(customerInstance){
13: def totalAwards = 0
14: customerInstance.awards.each{
15: totalAwards = totalAwards + it.points
16: }
17: customerInstance.totalPoints = totalAwards
Around line 33 of grails-app/controllers/rewards/CustomerController.groovy
30:
31: def show(Long id){
32: def customerInstance = Customer.get(id)
33: customerInstance = calculationsService.getTotalPoints(customerInstance)
34: [customerInstance: customerInstance]
35: }
36:
コントローラ (CustomerController.groovy)
package rewards
class CustomerController {
static scaffold = true
def calculationsService
def show(Long id){
def customerInstance = Customer.get(id)
customerInstance = calculationsService.getTotalPoints(customerInstance)
[customerInstance: customerInstance]
}
モデルのお客様
class Customer {
String firstName
String lastName
Long phone
String email
Integer totalPoints
static hastMany = [awards:Award, orders:OnlineOrder]
static constraints = {
phone()
firstName(nullable: true)
lastName(nullable: true)
email(nullable: true, email: true)
totalPoints(nullable: true)
}
}
モデル賞
package rewards
class Award {
Date awardDate
String type
Integer points
static belongsTo = [customer:Customer]
static constraints = {
}
}
サービス (CalculationsService.groovy)
package rewards
import grails.transaction.Transactional
@Transactional
class CalculationsService {
def serviceMethod() {
}
def getTotalPoints(customerInstance){
def totalAwards = 0
customerInstance.awards.each{
totalAwards = totalAwards + it.points
}
customerInstance.totalPoints = totalAwards
return customerInstance
}
}