Diving Deeper into Ecto Queries

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 == ...

Get Programming Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.