How to update

changeset = User.changeset(%User{}, %{email: “test@test.html”, password: “123456”})

user = User |> Repo.get_by(email: “admin@test.html”)

How to set use user.

My english is very bad. Please forgive me.

I assume you want to:

  1. fetch the User from the database
  2. apply changes (creating changeset)
  3. save it back to the database.

In such case you should do more or less (untested code):

user = User |> Repo.get_by(email: "admin@test.html")
changeset = User.changeset(user, %{email: "test@test.html", password: "123456"})
1 Like

Thank you for the comment.

I want this:

  def load_user(%Changeset{valid?: true} = changeset, field) do
    account = get_field(changeset, field)

    user = cond do
      String.match?(account, @regex_email) ->
        __MODULE__ |> Repo.get_by(email: account)
      String.match?(account, @regex_mobile) ->
        __MODULE__ |> Repo.get_by(mobile: account)
      true ->
        __MODULE__ |> Repo.get_by(username: account)

    cond do
      user ->
        IO.inspect user
        %{changeset | data: user}
      true -> changeset |> add_error(field, "no user")

But is empty

at which point the is empty?

It’s my fault. In shell it show emtpy.

I inspect, has data.

Thank you verify much.

Now, It’s work.