David B.
David B.

Reputation: 838

function is undefined or private while should be accessible

I'm trying to reach a method from my Accounts domain. I'm able to use any method from the Accounts domain in iex except for the mark_as_admin/1 (the last one).

here is the domain

defmodule Storex.Accounts do
  import Ecto.Query, warn: false
  alias Storex.Repo
  alias Storex.Accounts.User

  def create_user(attrs \\ %{}) do
    %User{}
    |> User.changeset(attrs)
    |> Repo.insert()
  end

  def get_user!(id) do
    Repo.get!(User, id)
  end

  def new_user() do
    User.changeset(%User{}, %{})
  end

  def authenticate_user(email, password) do
    Repo.get_by(User, email: email)
    |> User.check_password(password)
  end

  def mark_as_admin(user) do
    user
    |> User.admin_changeset(%{is_admin: true})
    |> Repo.update()
  end
end

Here's what I type in iex:

iex(1)> alias Storex.Accounts
iex(2)> user = Accounts.get_user!(7)
[debug] QUERY OK source="accounts_users" db=2.2ms
SELECT a0."id", a0."email", a0."full_name", a0."password_hash", 
a0."is_admin", a0."inserted_at", a0."updated_at" FROM "accounts_users" 
AS a0 WHERE (a0."id" = $1) [7]

%Storex.Accounts.User{__meta__: #Ecto.Schema.Metadata<:loaded, 
"accounts_users">,

email: "[email protected]", full_name: "Test User", id: 7,

inserted_at: ~N[2018-01-14 11:52:14.472928], is_admin: false, password: nil,

password_hash: 
"$2b$12$I7Kq8SgkWN77W3jx2QwaNe.so6z75xtYOIzl5Ws5kqlaTniLXMyIe",

updated_at: ~N[2018-01-14 11:52:14.472935]}

iex(3)> Accounts.mark_as_admin(user)
** (UndefinedFunctionError) function Storex.Accounts.mark_as_admin/1 is             
undefined or private
(storex) Storex.Accounts.mark_as_admin(%Storex.Accounts.User{__meta__:     
#Ecto.Schema.Metadata<:loaded, "accounts_users">, email: 
"[email protected]~ld",   full_name: "Test User", id: 7, inserted_at: 
~N[2018-01-14 11:52:14.472928], is_admin: false, password: nil, 
password_hash: 
"$2b$12$I7Kq8SgkWN77W3jx2QwaNe.so6z75xtYOIzl5Ws5kqlaTniLXMyIe", 
updated_at: ~N[2018-01-14 11:52:14.472935]})

Why can't I call this method ?

EDIT:
After having restarted iex the methods in the domain can't be called anymore:

iex(1)> alias Storex.Accounts  
Storex.Accounts  
iex(2)> Accounts.get_user!(7)  
** (UndefinedFunctionError) function Storex.Accounts.get_user!/1 is   
undefined or private  

Upvotes: 3

Views: 5386

Answers (1)

ryanwinchester
ryanwinchester

Reputation: 12137

You should start IEx from your project's root directory with something like

iex -S mix.

If you edit your code and save it, you should either restart IEx the same way, call recompile(), or reload any single modules you've changed that you want to use, like:

iex> recompile() # recompiles and reloads everything except mix.exs and configs

iex> r Storex.Accounts # recompiles and reloads a single module

Upvotes: 5

Related Questions