Insert and display multiple records from another table

Hello people! As a part of my elixir learning curve, I am working on the sneaker store application. I am struggling with displaying all sizes for each model. I was reading about Ecto.Multi but i don’t know if I’m going in the good direction. Another thing i do not know how to start with is adding available sizes for the exact model.
What I’d like to achieve is:

  1. For each shoe model (from the product table) display all available sizes and quantity of each size from the stock table.
  2. When adding a new shoe model, add sizes + quantity of each size in one go, so the admin doesn’t need to add each size and quantity separately for each shoe pair.

I will appreciate any help or guide in which direction I should go.

I think it would help if you share how your db schema looks like.

This should be straightforward if you have for example a “shoes” table, with a “size” and “quantity” rows. Maybe having a composite unique index between model and size.

EDIT: About your no. 2. That sounds like a UI feature

I guess I might mess up with my database design then…

I got two tables:

  schema "products" do
    field :colour, :string
    field :description, :string
    field :name, :string
    field :price, :decimal
    field :product_code, :string
    field :release_year, :integer

    belongs_to :category, Jordaniva.Categories.Category
    belongs_to :subcategory, Jordaniva.Categories.Subcategory
    has_many :items, Jordaniva.Inventory.Item
  schema "items" do
    field :quantity, :integer
    field :size, :decimal
    belongs_to :product, Jordaniva.Inventory.Product


So looks like you can do it with something like:

# Get products


# Item from product
Repo.get_by(Item, product_id: product_id)

# Both at the same time

Repo.get(Product, product_id) |> Repo.preload(:items)

# or...

  p in Product,
  where: == ^product_id,
  preload: [:items],
  # pagination logic...
|> Repo.all()

You can read more about it here:

And for your number 2: or

Here there’s a good article about it: and a convo Confussed with build_assoc vs put_assoc vs cast_assoc

Thank you! I will take a look into that.