Aguxez

Aguxez

My concern with working with associations in this case is having to update the record, I came up with a solution and would like feedback on it to see if it is correct or even viable.

I’m working on a project where users have inventories, obviously in their inventories they have items, I wanted to keep track of how much of an item an user has, so my schema goes as follows:

User:

  schema "users" do
    ...

    has_one(:inventory, Inventory)

    timestamps()
  end

Inventory:

  schema "inventories" do
    field(:item_quantities, {:array, :map}, default: [])
    belongs_to(:user, User)

    many_to_many(:items, Item, join_through: "inventory_items")

    timestamps()
  end

Item:

  schema "items" do
    ...

    many_to_many(:inventories, Inventory, join_through: "inventory_items")

    timestamps()
  end

The solution I came up with to keep track of the amount of a item an user has is with the field(:item_quantities, {:array, :map} part. With a common module I update this value so it is less error prone when updating the values (adding, removing, etc…) These are the relevant functions on the module:

  @doc """
  Updates 'item_id' on 'inventory' adding 'amount' to it.
  """
  def add_inventory_item_amount(%Inventory{} = inventory, item_id, amount) do
    updated_items =
      Enum.map(inventory.item_quantities, fn item ->
        case item["id"] == item_id do
          true ->
            %{item | "amount" => item["amount"] + amount}

          false ->
            item
        end
      end)

    inventory
    |> Ecto.Changeset.change(%{item_quantities: updated_items})
    |> Repo.update!()
  end

  @doc """
  Updates 'item_id' on 'inventory' removing 'amount' from it.
  """
  def remove_inventory_item_amount(%Inventory{} = inventory, item_id, amount) do
    updated_items =
      Enum.map(inventory.item_quantities, fn item ->
        case item["id"] == item_id do
          true ->
            %{item | "amount" => item["amount"] - amount}

          false ->
            item
        end
      end)

    inventory
    |> Ecto.Changeset.change(%{item_quantities: updated_items})
    |> Repo.update!()
  end

  @doc """
  Adds 'item_amount' to 'inventory' of 'item'. Mostly used to add new items to the inventory.
  """
  def add_item_to_inventory(%Item{} = item, %Inventory{} = inventory, item_amount) do
    params = %{id: item.id, amount: item_amount}

    inv = Repo.preload(inventory, :items)

    inv
    |> Ecto.Changeset.change(%{item_quantities: [params | inv.item_quantities]})
    |> Ecto.Changeset.put_assoc(:items, [item | inv.items])
    |> Repo.update!()
  end

My question is, is this a valid way to do what I want to do or can I work directly with the associations without keeping track on a separate field on the schema?

Showing Posts 1 to 2

jeremyjh

jeremyjh

I would make inventory_items a schema so it can have an amount attribute. Then you’d join through the schema rather than a raw table name e.g. many_to_many(:inventories, Inventory, join_through: InventoryItems). If you do this, I don’t think you actually need an inventories table, unless that will have some other attribute you don’t show (such as an inventory type or name).

Aguxez

Aguxez OP

Yes, this is what I was looking to do. I’m not discarding having an inventory table as we may have another use for it later on, but you’re right, as for now I don’t think it’d be 100% necessary. Thanks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews