April 2016
Beginner to intermediate
300 pages
6h 58m
English
So far, you know Ecto queries like a YouTube dog knows how to ride a bike. We’ve written our first query and we know that queries compose, but we still haven’t explored many concepts. It’s time to take off the training wheels and see more-advanced examples.
Open up IEx once more, and let’s retrieve a single user:
| | iex> import Ecto.Query |
| | iex> alias Rumbl.Repo |
| | iex> alias Rumbl.User |
| | |
| | iex> username = "josevalim" |
| | "josevalim" |
| | |
| | iex> Repo.one(from u in User, where: u.username == ^username) |
| | ... |
| | %Rumbl.User{username: "josevalim", ...} |
We’re using the same concepts you learned before:
Repo.one means return one row.
from u in User means we’re reading from the User schema.
where: u.username == ...