java - how to display a list based on a foreign key groovy grails -
i have 2 tables: region , district. region_id foreign key table district (a region has 1 or many districts). so, when select region on list want display districts associated particular region. correct code displays districts independently of region:
def list = { params.max = math.min(params.max? params.int('max') : 20, 100) [districtinstancelist : district.list(params), districtinstancetotal: district.count()] }  does know how display based on foreign key constraint? know write sql query in list closure, suppose grails has way it. database mysql, , grails version 2.0.1. district domain is:
class district { def scaffold = true string name string description string logo string homepage // defines 1:n constrain region table static belongsto = [region : region] // defines 1: constraint stream table static hasmany = [streams : stream] static constraints ={ name(blank:false, minsize:6, maxsize:30) description(blank: false, maxsize:100) } public string tostring(){ name } }  
you can use gorm:
def list = { params.max = math.min(params.max? params.int('max') : 20, 100) region region = region.get(params.id) // or parameter you're using list districts = district.findallbyregion(region) [districtinstancelist : districts, districtinstancetotal: district.count()] }  you can read grails gorm here: http://grails.org/doc/latest/guide/gorm.html
Comments
Post a Comment