Adding adress to user in phoenix/ecto

ecto
phoenix
troubleshooting
Tags: #<Tag:0x00007fbcac82ac88> #<Tag:0x00007fbcac82ab20> #<Tag:0x00007fbcac82a968>

#1

hey im trying to add adresses to users in my DB… I have gone through the getting started guide and association guide in hexdocs… but im still waay off… This is what i got so far.

User.ex

defmodule Squarestore.Identity.User do
    use Ecto.Schema
    import Ecto.Changeset

    schema "users" do
        field :fname, :string
        field :lname, :string
        field :phone, :string
        field :email, :string
        field :password, :string
        has_many :adresses, Squarestore.Identity.Adress
        timestamps()
    end

    @doc false
    def changeset(user, attrs) do
      user
      |> cast(attrs, [:fname, :lname, :phone, :email, :password])
      |> validate_required([:fname, :lname, :phone, :email, :password])  
    end
    
end    

adress.ex

defmodule Squarestore.Identity.Adress do
  use Ecto.Schema
  import Ecto.Changeset


  schema "adresses" do
    field :adress, :string
    field :city, :string
    field :country, :string
    field :postnr, :string
    belongs_to :user, Squarestore.Identity.User
    timestamps()
  end

  @doc false
  def changeset(adress, attrs) do
    adress
    |> cast(attrs, [:adress, :postnr, :country, :city])
    |> validate_required([:adress, :postnr, :country, :city])
  end
end

allso added an extra migration file called adress_belongs_to_user.exs

defmodule Squarestore.Repo.Migrations.AdressBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:adresses) do
      add :user_id, references(:users)
    end
  end
end

Hope anyone here can help me understand my problem. =)


#2

Nothing is jumping out as wrong to me here, are you seeing an error anywhere? What exactly is not working? If you want to be able make a changeset for a user together with it’s addresses, you can add a call to cast_assoc/3 in the user changeset function as documented here https://hexdocs.pm/ecto/Ecto.Changeset.html#cast_assoc/3


#3

I guess you are trying add addreses via one query.

defmodule Squarestore.Identity.User do
    use Ecto.Schema
    import Ecto.Changeset

    schema "users" do
        field :fname, :string
        field :lname, :string
        field :phone, :string
        field :email, :string
        field :password, :string
        has_many :adresses, Squarestore.Identity.Adress
        timestamps()
    end

    @doc false
    def changeset(user, attrs) do
      user
      |> cast(attrs, [:fname, :lname, :phone, :email, :password])
      |> cast_assoc(:adresses) #add this line
      |> validate_required([:fname, :lname, :phone, :email, :password])  
    end
    
end    

query example

Squarestore.Repo.insert(%Squarestore.Identity.User{fname: "fname", lname: "lname", email: "email@mail.com" , password: "12345678", adresses: [%{adress: "test address1", postnr: 1, country: "country 1", city: "city1"}, %{adress: "test address2", postnr: 2, country: "country 2", city: "city2"}]})

That should work if there is no typo mistake


#4

thnx, that worked :slight_smile: