Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

same thing db needs to be borrowed #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

phocks
Copy link

@phocks phocks commented Jan 19, 2023

No description provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @phocks, appreciate your attention to the tiny details.

The code snippet shown in the tutorial is copied from a actual Rust example. In the example the db is already a reference hence db but not &db. I want to keep it as is, adding a extra & seems trivial and Rust compiler is able to prompt user to do so.

// Relational Select
{
let la_boulangerie = bakery::ActiveModel {
name: ActiveValue::Set("La Boulangerie".to_owned()),
profit_margin: ActiveValue::Set(0.0),
..Default::default()
};
let bakery_res = Bakery::insert(la_boulangerie).exec(db).await?;
for chef_name in ["Jolie", "Charles", "Madeleine", "Frederic"] {
let chef = chef::ActiveModel {
name: ActiveValue::Set(chef_name.to_owned()),
bakery_id: ActiveValue::Set(bakery_res.last_insert_id),
..Default::default()
};
Chef::insert(chef).exec(db).await?;
}
// First find *La Boulangerie* as a Model
let la_boulangerie: bakery::Model = Bakery::find_by_id(bakery_res.last_insert_id)
.one(db)
.await?
.unwrap();
let chefs: Vec<chef::Model> = la_boulangerie.find_related(Chef).all(db).await?;
let mut chef_names: Vec<String> = chefs.into_iter().map(|b| b.name).collect();
chef_names.sort_unstable();
assert_eq!(chef_names, ["Charles", "Frederic", "Jolie", "Madeleine"]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage
Development

Successfully merging this pull request may close these issues.

2 participants