0

我已经能够让所有的父母和所有的孩子在一起。有人知道如何让单亲父母和所有的孩子在一起吗?

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)
}
4

1 回答 1

0

创建了一个元组;

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))
}
于 2019-06-25T09:57:56.077 回答