Debian Codegraph @

src/query_dsl/belonging_to_dsl.rs

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