Extract data from 2 different table linked

#1

Hi, how can I extract values from 2 different table, ServicePoint and Facility, where a ServicePoint is always linked to one Facility? I would a list of ServicePoint id and information about related Facility.

I would extract:
ID_SERVICE_POINT (field present in ServicePoint)
ADDRESS (field present in Facility)
CITY (field present in Facility)

In ServicePoint I have a field called facility where I have the id of the related Facility.

Thanks,
Laura

#2

Try with this:

c3Grid(ServicePoint.fetch({include: 'facility.address1, facility.city'}))

For instance, you can access those fields with:

var sps = ServicePoint.fetch({include: 'facility.address1, facility.city'}).at('objs')
var lines = sps.map(function(sp) {
  return [sp.id, sp.facility.address, sp.facility.city].join(',');
})
#3

Or you can try c3Grid(ServicePoint.fetch({include: ‘facility.this’}))

to retrieve all Facility attributes

#4

facility.this returns everything on facility: fields, calc fields, foreign attributes, etc. Serving such request may quickly became too slow. When it comes to fetch include as less as you can.

#5

In both the case it seems doesn’t work. The system returns only the ServicePoint “id” and the field “facility” in which I have (for example)
{
“type”: “Facility”,
“id”: “60”,
“HDDBase”: 20,
“CDDBase”: 26
}

#6

@lauraminicucci are the Facility.address1 and Facility.city stored calc?
in this case you need to refresh calc fields first

Facility.fetch({filter:“id==‘60’”,refreshCalcFields:true})

#7

Hey, the solution given from Bachir works

var sps = ServicePoint.fetch({include: ‘facility.address1, facility.city’}).at(‘objs’)
var lines = sps.map(function(sp) {
return [sp.id, sp.facility.address1, sp.facility.city].join(’,’);
})

Thanks!
Laura