Dave Anderson
Dave Anderson

Reputation: 755

GORM/Grails :: possible to query based on contents of a List within the model?

Assume the following:

class Thing {

    String name
    List<String> tags

    static constraints = {
        name(nullable: false)
        tags(nullable: false)
    }

}

I want to know if its possible, using GORM, to run a query for domain instances based on values in their respective lists

For instance: Are there dynamic GORM finders to query things like 'Find all Things that have the tag "Video" ', or 'Find all things with name = "Product1" that have the tag "Image" '

Just want to know if there's a nice concise way of doing this with Grails&Gorm, as opposed to retrieving a list of Things and iterating through it, finding the ones that have the appropriate tags and adding them to a results list.

Thanks!

Upvotes: 1

Views: 906

Answers (1)

David Genn
David Genn

Reputation: 709

One way (although not necessarily the most efficient!) would be to return the whole list of Things eg Thing.list() and then filter the resulting list using findAll.

List results = Thing.list().findAll{it.tags.contains("Image")}

How big is your list of Things and associated Tags likely to be?

Upvotes: 1

Related Questions