Merge pull request #68 from kdawgwilk/patch-1

Explain how to create many-to-many relationship
This commit is contained in:
Tanner 2016-10-31 16:07:40 -04:00 committed by GitHub
commit a2a29e5b27
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.