sfell77
sfell77

Reputation: 986

Grails: display linked table value in list.gsp instead of id

Let me preface this ask by indicating that this only applies to /list/ view and NOT other views. This is NOT about drop-down menus

I have two tables:

[Authors] - id - name

[Books] - id - title - author

In the Books form (create/edit) I can get the "author" field to display an author's name (from Author table instead of id) without an issue, however, when I view the /list/ view, it displays the author's id instead of the name (correctly, since this is what's stored in the DB).

I have not been able to find a method to convert "id" to "name" in the /list/ view elsewhere and I haven't used grails in almost a year (yay rust). Thanks in advance for guidance.

Upvotes: 0

Views: 614

Answers (1)

Hernán Erasmo
Hernán Erasmo

Reputation: 371

Try this:

class Author {

    String name

    static mapping = {
        id name: 'name'
    }
}

If you're using static scaffolding, don't forget to regenerate the views for the Author and Book domain classes:

grails> generate-views com.yourPackage.Author
grails> generate-views com.yourPackage.Book

Upvotes: 2

Related Questions