Hikaru Shindo
Hikaru Shindo

Reputation: 2913

Is it a bug of grails?

When I typed these code, it works perfectly.

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

But when I did like this, it shows error

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                 
}

But when I removed above code, the below was normally worked.

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

I didn't understand what's wrong ? Here's the error

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

Upvotes: 1

Views: 71

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122364

As explained in the other answer, the problem is the name clash between the users variable in the outer scope and the users {...} call inside the criteria closure. The simplest way to fix this is to rename the variable, but if that is not an option then an alternative fix is to use delegate. inside the closure:

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

This removes the ambiguity and forces Groovy to send the call to the closure delegate (the criteria builder) instead of the variable in the containing scope. You can use the same trick anywhere else where you have a name clash with a builder, I've had to use it in the past when creating XML with a MarkupBuilder.

Upvotes: 1

praveen_programmer
praveen_programmer

Reputation: 1062

You just replace

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

with

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

You have two different list one in hasMany, that name users and another defining in controller that creating confusion just change name . All code will work fine.

 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                 
    }

Upvotes: 1

Related Questions