我在grails项目的域类中有一个validFrom和validTo日期字段.我想在验证的基础上创建一个服务.任何人都可以告诉我如何检查它是否大于或小于当前的系统日期?
我有一个域,其中有两个字段可以为空,但不能同时为空。所以像这样的事情
class Character {
Association association
String otherAssociation
static constraints = {
association (validator: {val, obj-> if (!val && !obj.otherAssociation) return 'league.association.mustbeone'})
otherAssociation (validator: {val, obj-> if (!val && !obj.association) return 'league.association.mustbeone'})
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行如下测试时,我只会失败
void testCreateWithAssociation() {
def assoc = new Association(name:'Fake Association').save()
def assoccha = new Character(association:assoc).save()
assert assoccha
}
void testCreateWithoutAssociation() {
def cha = new Character(otherAssociation:'Fake Association').save()
assert cha
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑 看起来如果我将代码分解为如下所示:
def assoc = new Association(name:'Fake Association')
assoc.save()
Run Code Online (Sandbox Code Playgroud)
一切正常。但现在我想知道为什么我不能像在其他测试中那样将 .save() 放在同一行中并且它可以工作。