0

I've made a new Grails project with only:

TestDomain.class:

class TestDomain {

    String var1
    String var2

}

Bootstrap.groovy:

def init = { servletContext ->
    if (TestDomain.count() == 0) {
        new TestDomain(var1: "a", var2: "b").save()
        new TestDomain(var1: "a", var2: "c").save() 
    }
}

TestController.groovy:

def index() {
    def detachedCriteria = TestDomain.where { var1 == "a" }
    detachedCriteria = detachedCriteria.where { var2 == "b" }
    render detachedCriteria.list()
}


def indexWithMethod() {
    def detachedCriteria = TestDomain.where { var1 == "a" }
    detachedCriteria = addClause(detachedCriteria)
    render detachedCriteria.list()
}

def addClause(detachedCriteria) {
    detachedCriteria = detachedCriteria.where { var2 == "b" }
    return detachedCriteria
}

Calling index returns only 1 instance (as expected), but indexWithMethod returns both instances elements. Why isn't indexWithMethod not equivalent to index?

I'm using Grails 2.4.2, but this also happens in 2.3.6.

Bram Vonk
  • 11
  • 1
  • If you statically type the argument to your `addClause` method to be a `grails.gorm.DetachedCriteria` does that work? – Jeff Scott Brown Jul 11 '14 at 15:43
  • This isn't related to your problem but you shouldn't define a method like `def addClause(detachedCriteria)` in your controller because the compiler will treat that as a controller action, which it isn't. You should define that with something like `protected addClause(detachedCriteria)`. Only public methods are converted to actions. – Jeff Scott Brown Jul 11 '14 at 15:52
  • No, statically typing the return type does not help, neither does it if I make the argument statically typed. – Bram Vonk Jul 11 '14 at 22:24
  • (Oh, and yes, I know I should have made the internal method private (or protected? why not private?), but this was just an example project I made :)) – Bram Vonk Jul 11 '14 at 22:25

1 Answers1

0

This is a bug in Grails and GORM https://github.com/grails/grails-data-mapping/issues/982

dspies
  • 1,545
  • 14
  • 19