我目前正在使用带有 MongoDB 插件 (org.grails.plugins:mongodb:6.0.0.RC1) 的 Grails 2.5.4,每当我尝试更新任何域类的列表时,它都不起作用,例如:
投票等级:
class Votation {
String question
int minVotes
List <VoteOption> options
User owner
Chat chat
static belongsTo = [chat: Chat]
static embedded = ['options']
static constraints = {
owner nullable: false
chat nullable: false
//question nullable: false
}
投票选项类:
class VoteOption {
String option
String url
List <User> voters
static belongsTo = [chat: Chat]
}
当我尝试更新列表时:
//some more code...
Votation votation = Votation.findById(votationId as Long)
VoteOption option = votation.options.find { it.option == votationOption }
User user = User.findOrCreateNew(params.user)
if (option.voters) {
option.voters.add(user) // THIS DOESN'T WORK!
}
else {
option.voters = [user] //This DOES work
}
这只是一个例子,我还有 2 个域类也有列表,它们也不起作用。
重新启动 Grails 并不能解决这个问题,而且这也发生在其他开发人员的计算机上,所以这不是我的环境。其他所有内容都正确保存
最佳答案
Try this
//some more code...
Votation votation = Votation.findById(votationId as Long)
VoteOption option = votation.options.find { it.option == votationOption }
User user = User.findOrCreateNew(params.user)
if (user) {
option.addToVoters(user) // <----
}
option.save(flush:true, failOnError:true)
引用:http://docs.grails.org/2.1.0/ref/Domain%20Classes/addTo.html
关于mongodb - Grails MongoDB 列表不更新,我做错了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967012/