Linell
I’ve created an Opponent model that has several fields associated with it, each of which represents an actual or computed value. You can check out the whole file as it exists right now here, but the schema itself looks like this:
schema "opponents" do
field :name, :string
field :external_id, :string
field :away_losses, :float
field :away_wins, :float
field :home_losses, :float
field :home_wins, :float
field :losses, :float
field :neutral_losses, :float
field :neutral_wins, :float
field :opp_opp_winning_pct, :float
field :opp_winning_pct, :float
field :winning_percentage, :float
field :wins, :float
belongs_to :dataset, Scorcerer.Datasets.Dataset
timestamps()
end
Some of the fields are dependent on each other - for example for there to be a winning_percentage there have to be wins and losses. Others can bet set or not and it doesn’t really matter that much - we can compute wins from home_wins and away_wins even if neutral_wins is nil. Right now I’ve got it setup to handle all of that via functions like this:
def autosum_fields(opponent) do
if opponent.wins == nil do
set_wins(opponent)
end
if opponent.losses == nil do
set_losses(opponent)
end
if opponent.winning_percentage == nil do
set_winning_percentage(opponent)
end
end
defp set_winning_percentage(opp) do
if (opp.wins != nil && opp.losses != nil) do
Scorcerer.Opponents.update_opponent(opp, %{ winning_percentage: opp.wins / opp.losses })
end
end
defp set_wins(opp) do
wins = Enum.reduce([:home_wins, :away_wins, :neutral_wins], 0, fn key, acc ->
key_value = Map.get opp, key
acc + (key_value || 1)
end)
Scorcerer.Opponents.update_opponent(opp, %{ wins: wins })
end
defp set_losses(opp) do
losses = Enum.reduce([:home_losses, :away_losses, :neutral_losses], 0, fn key, acc ->
key_value = Map.get opp, key
acc + (key_value || 1)
end)
Scorcerer.Opponents.update_opponent(opp, %{ losses: losses })
end
I’ve actually got two questions here:
- What is the right way to ensure that my
autosum_fieldsmethod runs whenever the opponent is updated? - Is there a better way to handle the actual updates? I know I’m doing more updates than is required right now because it’s happening on a per-field basis.
Trending in Questions
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
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
shankardevy
get_fieldgets the value from changeset and if the field is not present in changes, then it gets it from the record.hauleth
Assuming PostgreSQL as a database.
You have 2 options how to make it “right”:
Both will make your application much clearer and, at least logically, normalise your data.
Last Post!
al2o3cr
Yes - if the fields are writable directly, you’ll need to decide what happens when incompatible parameters are assigned; for instance,
%{home_losses: 10, away_losses: 5, losses: 3}. Should that ignorelosses?Also consider if directly writing to the fields (even ones like
home_losses) is the best approach; if data arrives incrementally (one result at a time) you might instead want operations like “record this game was a win at home” that manipulate multiple fields and recalculate things likewinning_percentage.