BoredT
BoredT

Reputation: 1590

Grails - showing the right class name

I have simple hierarchy:

class Atom {}

class Text extends Atom { String value}

class Unit { 
    List atoms
    static hasMany = [ atoms:Atom ] 
}

Let's say we create one Text object and one Unit object, and select Unit as a parent of Text. It works fine. When I call show method on Unit, I see that my Unit consists of one Atom. When I clicking on this atom, it redirects me to atom controller, but my atom is actually text, and I want to see Text controller. It is expected behavior, because in my show.gsp there is such code:

<g:each in="${unitInstance.atoms}" var="a">
    <span class="property-value" aria-labelledby="atoms-label">
        <g:link controller="atom" action="show" id="${a.id}">
            ${a?.encodeAsHTML()}
        </g:link>
    </span>
</g:each>

Okay, database stores the actual atom type in field "class", so I edited the line 3:

<g:link controller="${a.class}" action="show" id="${a.id}">    

And ${a.class} always return atom. So, my question is - how can we get actual class name?

Upvotes: 0

Views: 202

Answers (2)

schmolly159
schmolly159

Reputation: 3881

You are probably hitting the GORM Gotcha about Hibernate returning proxies instead of inflated objects.

In your link you could try:

<g:link controller="${Atom.get(a.id).getClass()}" ... />

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

There may be a better way, but how.about adding a method in Atom and overriding it in Text, that returns the appropriate string?

Upvotes: 1

Related Questions