Skip to content

Instantly share code, notes, and snippets.

@profh
Created June 8, 2016 15:33
Show Gist options
  • Save profh/f17e4dd0f238ce10f8724fdd91d30256 to your computer and use it in GitHub Desktop.
Save profh/f17e4dd0f238ce10f8724fdd91d30256 to your computer and use it in GitHub Desktop.
Using iex with BookManager app
### Stepping through class example on BookManager
# Import Ecto.Query first, or none of this works in iex:
import Ecto.Query
# A simple example
query = from BookManager.Book
BookManager.Repo.all(query)
# Creating aliases because BookManager is just to long to keep rewriting:
alias BookManager.Publisher
alias BookManager.Author
alias BookManager.Book
alias BookManager.BookAuthor
alias BookManager.Repo
# Looking at the example above again:
query = from BookManager.Book
Repo.all(query)
Repo.all(Book)
# ... gives me the same result
# Building up queries:
Book |> Book.alphabetical
Book |> Book.alphabetical |> Book.this_century
# Now execute the query on our repo:
Book |> Book.alphabetical |> Book.this_century |> Repo.all
# Get a specific book: (! raises exception, no ! will return nil if missing)
dc = Repo.get_by!(Book, title: "The Divine Comedy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment