Search code examples
elixirphoenix-frameworkecto

UndefinedFunctionError - iex aliasing in phoenix / ecto


when i launch

iex -S mix phoenix.server

I would like to be able to run commands like:

iex(1) > Repo.all(MyModel)

However, this gives me this error:

(UndefinedFunctionError) undefined function: Repo.all/1 (module Repo is not available)

If I prefix my calls with my ProjectName, it works:

iex(1) > ProjectName.Repo.all(ProjectName.MyModel)

How can I avoid having to prefix my calls with my Project Name in iex?


Solution

  • If you add code to the file .iex.exs, it'll get executed whenever you launch iex in that directory. So if you just add this to your .iex.exs:

    alias ProjectName.{Repo, MyModel}
    

    you'll be able to access ProjectName.Repo as Repo and ProjectName.MyModel as MyModel.