Call namedQuery inside the criteria in the controller

Is it possible to call namedQuery on grails inside the controller? I know that I can call namedQuery inside another namedQuery, but I don't want to do this. Any ideas? Thanks

User .groovy

static namedQueries = { filterUsers{ eq("age", 21) } } 

MyController .groovy

 def r = User.createCriteria().list { eq("id", 1) filterUsers() //not possible } 

or..

MyController .groovy

  //not possible too //Cannot invoke method createCriteria() on null object def r = User.filterUsers().createCriteria().list { eq("id", 1) } 
+4
source share
1 answer

Here is an example:

Domain:

 class User { int age String userName static namedQueries = { filterUsers { eq("age", 21) } } static constraints = { } } 

Controller:

 class TestController { def index = { def users = User.filterUsers { and { like 'userName', 'Derek%' } } render users as JSON } } 

In addition, you can find more information about this here: Help documentation

+4
source

All Articles