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.