diesel/query_dsl/
belonging_to_dsl.rs

1/// Constructs a query that finds record(s) based on directional association with other record(s).
2///
3/// # Example
4///
5/// ```rust
6/// # #[macro_use] extern crate diesel;
7/// # include!("../doctest_setup.rs");
8/// # use schema::{posts, users};
9/// #
10/// # #[derive(Identifiable, Queryable)]
11/// # pub struct User {
12/// #     id: i32,
13/// #     name: String,
14/// # }
15/// #
16/// # #[derive(Debug, PartialEq)]
17/// # #[derive(Identifiable, Queryable, Associations)]
18/// # #[belongs_to(User)]
19/// # pub struct Post {
20/// #     id: i32,
21/// #     user_id: i32,
22/// #     title: String,
23/// # }
24/// #
25/// # fn main() {
26/// #     run_test();
27/// # }
28/// #
29/// # fn run_test() -> QueryResult<()> {
30/// #     let connection = establish_connection();
31/// #     use users::dsl::*;
32/// #     use posts::dsl::{posts, title};
33/// let sean = users.filter(name.eq("Sean")).first::<User>(&connection)?;
34/// let tess = users.filter(name.eq("Tess")).first::<User>(&connection)?;
35///
36/// let seans_posts = Post::belonging_to(&sean)
37///     .select(title)
38///     .load::<String>(&connection)?;
39/// assert_eq!(vec!["My first post", "About Rust"], seans_posts);
40///
41/// // A vec or slice can be passed as well
42/// let more_posts = Post::belonging_to(&vec![sean, tess])
43///     .select(title)
44///     .load::<String>(&connection)?;
45/// assert_eq!(vec!["My first post", "About Rust", "My first post too"], more_posts);
46/// #     Ok(())
47/// # }
48/// ```
49pub trait BelongingToDsl<T> {
50    /// The query returned by `belonging_to`
51    type Output;
52
53    /// Get the record(s) belonging to record(s) `other`
54    fn belonging_to(other: T) -> Self::Output;
55}