tozz

tozz

I’m pretty sure I’m in XY-Problem land right now, but here goes (I have shortened the code as much as possible to keep it focused).

I have a Ecto schema (SubMetric), with a simple
has_many :values, SubMetricValues, on_replace: :delete

I also have a list that is dynamic in nature, let’s say it contains ["a", "b", "c"] called configured_ranges below.

Now I want to make sure that when creating a new SubMetric, or editing one the values are in sync based on the list. In my SubMetric changeset I to the usual casting etc and then I have a function specifically for this case (I figured keeping this in the schema is the best place to avoid code duplication or forgetting to trigger the sync behavior).

def changeset(sub_metric, attrs \\ %{}) do
    sub_metric
    |> cast(attrs, [:metric_id, :name, :identifier, :static])
    |> validate_required([:metric_id, :name, :identifier, :static])
    |> cast_assoc(:values)
    |> synchronize_ranges()
  end

  defp synchronize_ranges(changeset) do
    current_values = get_field(changeset, :values, [])
    configured_ranges = ["a", "b", "c"]
    # The "range" key is one of the values from the configured_ranges
    current_ranges = Enum.map(current_values, & &1.range)

    updated_values =
      current_values
      |> Enum.filter(&(&1.range in configured_ranges))
      |> then(fn existing_values ->
        new_ranges = configured_ranges -- current_ranges
        new_values = Enum.map(new_ranges, &%{range: &1, value: 0})
        existing_values ++ new_values
      end)

    put_assoc(changeset, :values, updated_values)
    # Here the changeset is empty when editing, no changes are registered, which means the form later in the interface gets reset to the values it had when created.
  end

Any ideas? It’s a bit of a strange case I guess, adding dynamic fields from scratch using sort params etc are simple, but here I need the database to contain a specific set of values based on external sources.

Showing Posts 1 to 3

dimitarvp

dimitarvp

Try not using cast_assoc at all? You’re overriding it immediately anyway by using put_assoc right after.

tozz

tozz OP

That won’t work, I need it for validation and making the proper changesets from the values. And it’s not necessarily so that any changes have been done on the values.

I actually managed to make it work by changing the synchronize_ranges function into

defp synchronize_age_ranges(changeset) do
    current_values = get_field(changeset, :values, [])
    configured_ranges = dynamic_list_of_ranges()
    current_ranges = Enum.map(current_values, & &1.range)

    if current_ranges -- configured_ranges != [] || configured_ranges -- current_ranges != [] do
      updated_values =
        current_values
        |> Enum.filter(&(&1.range in configured_ranges))
        |> then(fn existing_values ->
          new_ranges = configured_ranges -- current_ranges
          new_values = Enum.map(new_ranges, &%{range: &1, value: 0})
          existing_values ++ new_values
        end)

      put_assoc(changeset, :values, updated_values)
    else
      changeset
    end
  end

Still feels a bit cumbersome, but this covers new entries, existing ones without modification of the ranges and when removing/adding a range and editing it (or not).

The easier way would probably to be use a Multi, I don’t find this code very readable. But still curious as if to there’s better ways, can’t be that rare that you want to populate rows based on something from “outside” the context of the database.

tozz

tozz OP

Here’s an English example, to avoid looking at my crazy code :smiley:

You have a Game
Each Game have Participants
Participants can join and leave games, when they leave their score is removed and when they join it’s set to zero.
The Partipants are supplied to the Game from an external resource.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews