我已经能够让所有的父母和所有的孩子在一起。有人知道如何让单亲父母和所有的孩子在一起吗?
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)
}