Syrianrevo
Syrianrevo

Reputation: 31

how to show data on the gsp screen from multiple tables that share a common value?

lets say I have three tables

when the user create a Trips record, he is required to create TripLegs and TripStates as well.

So my question is on edit screen, how can I get the data from TripLegs and TripStates that when their trip_id is equal to id for the Trips column.

def edit() {
    def TripsInstance = Trips.get(params.id)

     def tripId = new Integer(TripsInstance.id) Storing the TripInstanceID                   
     def tripLegsInstance =       

     TripLegs.findByTripId(tripId?.toString().toInteger(),  
     [sort:'tripNumber']) 




     def tripStatesInstance =  
     TripStates.findByTripId(tripId?.toString().toInteger(),
     [sort:'tripNumber'])

okay so my code works fine, I can retrieve data from three tables to the edit screen but I am struggling on the update method, how do I get the new instances stored?

Upvotes: 1

Views: 153

Answers (2)

Meni Lubetkin
Meni Lubetkin

Reputation: 138

Please add the domain model and association code for:

  • Trips
  • TripLegs
  • TripStates

based on you code example I can assume both TripLegs and TripStates are belongsTo Trip(s) and the relationship is One-to-many. if so you can access the TripLegs and TripStates by querying the Trip by Id

Trip myTrip = Trip.get(id)
myTrip.legs 
myTrip.states

if you add the domain model and association code you can get more specific help

Upvotes: 1

quindimildev
quindimildev

Reputation: 1280

You have to create the finders for TripLegs and TripStates

Trips tripsInstance = Trips.get(params.id)

//This criteria returns a list of tripLegs related with trip
List tripLegs = TripLegs.findAllByTrips(tripsInstance)

//This criteria returns a list of tripStates related with trip
List tripStates = TripStates.findAllByTrips(tripsInstance)

Now you have a Trip, and their TripLegs and TripStates in two list.

Return them to the gsp this way in your edit action

[trip: tripsInstance, tripLegs: tripLegs, tripStates: tripStates]

and you can show the information on your gsp with a <g:each> for each list.

<g:each in="${tripLegs}" var="tripLeg"/>

Note that I assumed that your classes are something like this:

TripState{
   Trips trips //it is mapped as trip_id
}

Upvotes: 0

Related Questions