I run some code with iex -S mix
This is ok:
user = Repo.get User, 1
Ecto.Changeset.change user, %{name: "xxxx"}
but this is wrong:
User.change user, %{name: "xxxx"}
raise (UndefinedFunctionError) undefined function Rumbl.User.change/2
I notice there is import Ecto.Changeset
in function model in file web.ex
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
end
end
So I think User.change/2 should works as same as Ecto.Changeset.change/2, is it right?
I think you may be misunderstanding how import
works.
From the docs:
Imports functions and macros from other modules.
import allows one to easily access functions or macros from others modules without using the qualified name.
What this means is that you don't have to use Ecto.Changeset.change(user, %{}
inside of the module, you can instead do change(user, %{})
.
This change only imports the functions to the module being used. It does not define them as functions on the model that is imported into.
If you are in iex and don't want to type out the fully qualified function name, you can either do:
alias Ecto.Changeset
Changeset.change(user, %{})
or:
import Ecto.Changeset
change(user, %{})