I want to POST for association one to many with JSON in Phoenix Elixir. There are many examples on the web, but I have not seen one example with one to many association. How can I pass the parameters for contact in the controller?
The schema for Costumer is
schema "customers" do
field :email, :string
field :name, :string
has_many :contacts, App.Customers.Contact
timestamps()
end
@doc false
def changeset(customer, attrs \\ %{}) do
customer
|> cast(attrs, [:name, :email])
|> validate_required([:name, :email])
|> unique_constraint(:email)
end
The schema for Contact is
schema "contacts" do
field :phone, :string
belongs_to :customers, App.Customers.Customer, foreign_key: :customer_id
timestamps()
end
@doc false
def changeset(contact, attrs \\ %{}) do
contact
|> cast(attrs, [:phone])
|> validate_required([:phone])
end
This is the controller:
def create(conn, %{"email" => email, "name" => name, "phone" => phone} = customer_params) do
with {:ok, %Customer{} = customer} <- Customers.create_customer(customer_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.customer_path(conn, :show, customer))
|> render("show.json", customer: customer)
end
end