0

I have a table called 'District' in my database:

+----+---------+-------------+----------+------+------------------------------------------------+-----------+
| id | version | description | homepage | logo | name                                           | region_id |
+----+---------+-------------+----------+------+------------------------------------------------+-----------+

so, on my controller i have:

def show = {
        def districtInstance = District.get(params?.id)
        println districtInstance
        println params
        if(!districtInstance){
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'district.label', default: 'District'), params.id])}"
            redirect(action : "list", params?.id)
        }
        else{
            [districtInstance : districtInstance]
        }
    }

While I'm expecting to see the full row:

| 71 |       0 | bogus  |          |      | bogus                                      |         4 |

I only see:

bogus

on the console. How can I have access to the entire row? (Specially districtInstance.region_id? )

cybertextron
  • 10,547
  • 28
  • 104
  • 208

2 Answers2

2

println districtInstance only prints the output of the toString() method. Use println districtInstance?.dump() to see more output including all of the class fields. It won't look like the database view however, since it's a Groovy object, not a database query result.

Update based on question: If you need the region_id, you can get it via districtInstance.regionId which doesn't require an extra database call.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
1
println districtInstance?.region.id

In Groovy, and object oriented languages in general, you can to navegate class properties like that, but you should are aware of the Law of Demeter

Community
  • 1
  • 1
AA.
  • 4,496
  • 31
  • 35