Дизель принесет одного родителя и много детей - ракета - PullRequest
0 голосов
/ 25 июня 2019

Мне удалось собрать всех родителей вместе со всеми детьми. Кто-нибудь знает, как получить одного родителя со всеми детьми?

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

1 Ответ

0 голосов
/ 25 июня 2019

создал кортеж;

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))
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...