这是grails的错误吗?

Is it a bug of grails?

当我输入这些代码时,它运行良好。

def users = User.getAll(params.userChecked)*.id 
println 'user : ' + users 

但是当我这样做的时候,它显示错误

def users = User.getAll(params.userChecked)*.id 
println 'user : ' + users 

def x = [1, 2]
x.each(){
  def roles = Role.withCriteria{
      users {
             eq 'id', new Long(80)
            }  
  }  
  println roles                 
}

但是当我删除上面的代码时,下面的代码正常工作。

def x = [1, 2]
x.each(){
  def roles = Role.withCriteria{
      users {
             eq 'id', new Long(80)
            }  
  }  
  println roles                 
}

我不明白怎么了?这是错误

No signature of method: java.util.ArrayList.call()...

你只需更换

def users = User.getAll(params.userChecked)*.id 

 def usersList = User.getAll(params.userChecked)*.id 

您有两个不同的列表,一个在 hasMany 中,名称为 users,另一个在控制器中定义,创建混淆只是更改名称。所有代码都可以正常工作。

 def users = User.getAll(params.userChecked)*.id 
    println 'user : ' + users 

    def x = [1, 2]
    x.each(){
      def roles = Role.withCriteria{
          users {
                 eq 'id', new Long(80)
                }  
      }  
      println roles                 
    }

中所述,问题是外部作用域中的 users 变量与 criteria 闭包中的 users {...} 调用之间的名称冲突。解决这个问题的最简单方法是重命名变量,但如果这不是一个选项,那么另一种解决方法是在闭包内使用 delegate.

def roles = Role.withCriteria{
  delegate.users {
    eq 'id', new Long(80)
  }
}

这消除了歧义并强制 Groovy 将调用发送到闭包委托(标准构建器)而不是包含范围中的变量。您可以在与构建器名称冲突的任何其他地方使用相同的技巧,我过去在使用 MarkupBuilder.

创建 XML 时不得不使用它