在groovy中显示名称而不是id

时间:2012-07-05 01:21:16

标签: grails groovy

我正在开发一个项目,其中有两个表regiondistrict。在region我有:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | bigint(20) | NO   | PRI | NULL    | auto_increment |
| name        | blob       | YES  |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+

并在district我有:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | bigint(20) | NO   | PRI | NULL    | auto_increment |
| name        | blob       | YES  |     | NULL    |                |
| region_id   | bigint(20) | NO   | MUL | NULL    |                |
+-------------+------------+------+-----+---------+----------------+

在区域视图中,显示区域的名称,但不显示区域名称:

Id  1
Name    Community College
Description Community College
Homepage    
Logo    
Region  ctv.Region : 1
Streams 

这是show中的DistrictController

def show = {
        def districtInstance = District.get(params.id)

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

如何修改我的show关闭,而不是显示Region.id而是region.name? 谢谢

1 个答案:

答案 0 :(得分:3)

我假设您刚刚使用脚手架?因此,尝试覆盖Region中的toString(),例如

public String toString() {
   name
}