我已经能够让所有的父母和所有的孩子在一起 有谁知道如何让所有子女都成为单亲父母?
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)
}
答案 0 :(得分: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))
}