Search code examples
grailsgrails-orm

Query associated domain object with params gives error: Cannot query property "params" - no such property on class


I have two domain objects: HeadOfHousehold, and Address,

In HeadOfHousehold I have the following relationship:

Address currentAddress
Address previousAddress

In Address I have:

static belongsTo = [headOfHousehold: HeadOfHousehold]

I have the following query:

def customers = HeadOfHousehold.findAll {
            if(params.lastName){
                lastName =~ params.lastName+ "%"
            }
            if(params.firstName){
                firstName =~ params.firstName + "%"
            }
            if(params.middleName){
                middleName =~ params.middleName + "%"
            }
            if(params.city){
                currentAddress.city =~ params.city+ "%"
            }
            if(params.zipCode){
                currentAddress.zipCode == params.zipCode
            }
            if(params.extendedZipCode){
                currentAddress.extendedZipCode == params.extendedZipCode
            }

        }

The problem is on those final three statements I get the error:

Cannot query property "params" - no such property on class HeadOfHousehold

It does not complain about the queries associated with lastName firstName or middleName, just when I go into the address. I can get around it by saving the params in a string and going that way:

String city = params.city + "%"
        String zipCode = params.zipCode
        String extendedZipCode = params.extendedZipCode
        String state = params.state
        def customers = HeadOfHousehold.findAll {
            if(params.lastName){
                lastName =~ params.lastName+ "%"
            }
            if(params.firstName){
                firstName =~ params.firstName + "%"
            }
            if(params.middleName){
                middleName =~ params.middleName + "%"
            }
            if(params.city){
                currentAddress.city =~ city 
            }
            if(params.state){
                currentAddress.state == state
            }
            if(params.zipCode){
                currentAddress.zipCode == zipCode
            }
            if(params.extendedZipCode){
                currentAddress.extendedZipCode == extendedZipCode
            }

        }

Why is that necessary? What am I doing wrong? (using grails version 2.3.8)


Solution

  • Trying to match against the property of an association or of a property, it looks params as a property on the parent class. I am not fully sure if this is an expected behavior. I will look into it more.

    But you can very well surmount this problem by using the way you did or just use the closure approach (DSL way) compared to . operation. For example:

    if(params.city){
        currentAddress { city =~ params.city+ "%" }
    }
    if(params.zipCode){
        currentAddress { zipCode == params.zipCode } 
    }
    if(params.extendedZipCode){
        currentAddress { extendedZipCode == params.extendedZipCode }
    }