-1

I've been able to get all parents along with all children Anyone has an idea how to get a single parent with all children?

pub struct CityAndNeighbourhoods(CustomerCity, Vec<Vec<CustomerNeighbourhood>>);

pub fn get_customer_city_with_neighbourhoods(
    id: i64,
    connection: &PgConnection,
) -> QueryResult<CityAndNeighbourhoods> {
    // Load a single customer_citys given an id
    let city = customer_citys::table
        .find(id)
        .first::<CustomerCity>(&*connection)
        .expect("Error loading city");
    // Load all customer_neighbourhoods belong to the customer_citys above
    // and group by customer_citys
    let neighbourhoods =
        CustomerNeighbourhood::belonging_to(&city).load::<CustomerNeighbourhood>(connection)?;
    // Return all customer_citys with them customer_neighbourhoods
    let data = CityAndNeighbourhoods(city, neighbourhoods.into_iter().zip().collect::<Vec<_>>());
    Ok(data)
}
moh_abk
  • 2,064
  • 7
  • 36
  • 65

1 Answers1

0

created a tuple;

pub fn get_customer_city_with_neighbourhoods(
    id: i64,
    connection: &PgConnection,
) -> QueryResult<(CustomerCity, Vec<CustomerNeighbourhood>)> {
    // Load a single customer_citys given an id
    let city = customer_citys::table
        .find(id)
        .first::<CustomerCity>(&*connection)
        .expect("Error loading city");
    // Load all customer_neighbourhoods belong to the customer_citys above
    // and group by customer_citys
    let neighbourhoods =
        CustomerNeighbourhood::belonging_to(&city).load::<CustomerNeighbourhood>(connection)?;
    // Return all customer_citys with them customer_neighbourhoods
    Ok((city, neighbourhoods))
}
moh_abk
  • 2,064
  • 7
  • 36
  • 65