O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required