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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Try not using
cast_assocat all? You’re overriding it immediately anyway by usingput_assocright after.tozz
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_rangesfunction intoStill 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
Here’s an English example, to avoid looking at my crazy code
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.