Question

Good evening, I'm creating a bookmark app from the book The Definitive Guide to Grails by Graeme Rocher. In this app I have 4 domains: User, Bookmark, Tag and TagReference. The Bookmark

hasMany = [tags:TagReference]
def belongsTo = User
User user   

URL url
String title
String notes

The TagReference

Bookmark bookmark
User user
Tag tag

The Tag

String name

I'm trying to search bookmarks not only by title and notes, but also by tags.

Bookmark Controller:

def search = {
    def criteria = Bookmark.createCriteria()
    def b = criteria.list {
                or {
                    ilike('title',"%${params.q}%".toString())
                    ilike('notes',"%${params.q}%".toString())

                    tags {
                        ilike('name',"%${params.q}%".toString())}
                    }
            }

           def fromDelicious = null
           try {
               fromDelicious = deliciousService.findRecent(session.user)
           }
           catch(Exception e) {
               log.error('Failed to invoke del.icio.us: ' + e.message, e)
           }
        render(view:'list',model:[bookmarkInstanceList:b.unique(),deliciousResults:fromDelicious])

/layouts/main

<g:form controller="bookmark" action="search" >
<input type="text" name="q" /> <input type="submit" value="search" />

If I remove the tags part it works fine (only for searching by title and notes of course). But with it, I get this error:

Executing action [search] of controller [bookmarks.BookmarkController] caused exception: could not resolve property: name of: bookmarks.TagReference

I understand that's because my TagReference doesn't have the attribute name, but how can I get it?

Was it helpful?

Solution

Given your domain model you need to access the Tag.name property through the tags collection. So you criteria would look something like this:

def criteria = Bookmark.createCriteria()
def b = criteria.list {
            or {
                ilike('title',"%${params.q}%".toString())
                ilike('notes',"%${params.q}%".toString())

                tags {
                    tag {
                        ilike('name',"%${params.q}%".toString())}
                    }
                }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top