I’ve been working on my first Phoenix project recently. I found it very annoying to have to add an aliases for every model I want to interact with in iex
. There’s an easier way…
What I was doing
Let’s say I want to view all the Article
s in my database. I would load up iex
by running ‘iex -S mix
’ and then run:
alias MyApp.Repo
alias MyApp.Article
Repo.all(Article)
Using an .iex.exs
file
I recently discovered that you can use a local .iex.exs
project to set your aliases. For more on using an .iex.exs
file, see the iex docs.
Create an .iex.exs
file in the root of your project and add the aliases you need:
alias MyApp.Repo
alias MyApp.User
alias MyApp.Team
alias MyApp.Article
Now when you load up your iex
shell using ‘iex -S mix
’ your aliases will already be set. To view all the articles we can just run:
Repo.all(Article)
Shorthand
As of Elixir 1.2 you can alias multiple submodules in one call
alias MyApp.{Repo, User, Team, Article}