Associations

suryeshchatwani

New member
I am a grails newbie and for days i have been trying to resolve this:
i am using the tutorial on IBM, its not my code, but i am trying to add something, but it isnt quite working.


Original Airline class

class Airline{
static constraints = {
name(blank:false, maxSize:100)
url(url:true)
frequentFlyer(blank:true)
notes(maxSize:1500)
numberOfTrips(default: 0)
}

String name
String url
String frequentFlyer
String notes

static hasMany = [trip:Trip]

String toString(){
return name
}


Original Trips Class

class Trip {
static constraints = {
airline()
name()
city()
startDate()
endDate()
purpose(inList:["Training", "Conference", "Consulting", "Other"])
notes(maxSize:2000)
}

String name
String city
Date startDate
Date endDate
String purpose
String notes
Airline airline
}


So basically i wanted to add a feature in airline class to be able to calculate number of trips made by this airline.
I made these changes, i am fairly new to grails, so maybe i havent quite undertood something, waiting for your responses!
class Airline{
static constraints = {
name(blank:false, maxSize:100)
url(url:true)
frequentFlyer(blank:true)
notes(maxSize:1500)
numberOfTrips(default: 0)
}

String name
String url
String frequentFlyer
String notes
String numberOfTrips

static hasMany = [trip:Trip]

static transients = ['numberOfTrips']

String toString(){
return name
}

int getNumberOfTrips(){
if(this.name!=null)
{
def tempTrips = Trip.findAll(this.name)
if(tempTrips!=null)
return tempTrips.count()
else
return 0
}
else
return 0
}
}
 
Top