April 2019
Beginner to intermediate
244 pages
6h 34m
English
The Repo.update_all and Repo.insert_all operations, first discussed in Chapter 1, Getting Started with Repo, allow you to update and insert large groups of records with a single query. Although they’re less commonly used than their counterparts update and insert, they can be very efficient for certain use cases.
When using Repo.update, you need to perform one query for each record you want to update. Let’s look at how we might reset the number_of_plays counter on our Track records back to 0:
| | tracks = Repo.all(Track) |
| | Enum.each(tracks, fn track -> |
| | track |
| | |> Ecto.Changeset.change(%{number_of_plays: 0}) |
| | |> Repo.update!() |
| | end) |
If our database had 1000 tracks, we’d need to run 1000 ...
Read now
Unlock full access