Explain how to create many-to-many relationship

This commit is contained in:
Kaden Wilkinson 2016-10-31 01:13:30 -06:00 committed by GitHub
parent e0c0cc8852
commit 687ff18eed
1 changed files with 17 additions and 1 deletions

View File

@ -131,7 +131,7 @@ extension Pet {
And the opposite for `Toy`.
```
```swift
extension Toy {
func pets() throws -> Siblings<Pet> {
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>(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.