From 687ff18eedc374257ed3a3a343b8b682e25c33a5 Mon Sep 17 00:00:00 2001 From: Kaden Wilkinson Date: Mon, 31 Oct 2016 01:13:30 -0600 Subject: [PATCH] Explain how to create many-to-many relationship --- fluent/relation.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/fluent/relation.md b/fluent/relation.md index 1947767d..56065abc 100644 --- a/fluent/relation.md +++ b/fluent/relation.md @@ -131,7 +131,7 @@ extension Pet { And the opposite for `Toy`. -``` +```swift extension Toy { func pets() throws -> Siblings { return try siblings() @@ -146,6 +146,22 @@ let pet: Pet = ... let toys = pet.toys().all() ``` +To create a new many-to-many relationship you can do the following. + +```swift + +var toy: Toy = ... // Create a new toy +try toy.save() // Save the toy to the db + + +var pet: Pet = ... // Create a new pet +try pet.save() // Save the pet to the db + +// Link them together in the db +var pivot = Pivot(toy, pet) // Create the relationship +try pivot.save() // Save the relationship to the db +``` + ### Preparation To prepare for a relationship with a `Pivot`, simply add the pivot to the `Droplet`'s preparations.