agonzalezro

agonzalezro

Hi folks :wave:

TLDR; I have an idea explained at the end, but I am not sure that’s how I should be doing it in Ash.

I am trying to understand how my domains & resources should look like.

I have a domain Portofolio where I want to store Assets and the price of those Assets for a given date.

A simplified version of what I have is:

defmodule Portfolio.Asset do
  attributes do
    ...
    attribute :name, :string, allow_nil?: false
    ...
  end

  relationships do
    belongs_to :user, Accounts.User, allow_nil?: false
    has_many :snapshots, AssetSnapshot
  end
end

defmodule Portfolio.AssetSnapshot do
  attributes do
    ...
    attribute :amount, :decimal, allow_nil?: false
    create_timestamp :created_at
    ...
  end

  relationships do
    belongs_to :asset, Portfolio.Asset, allow_nil?: false
  end
end

With these resources I could “easily” get user.assets and then for each asset a asset.last_snapshot or even have a calculation in the Portfolio for it. But at the moment that I need a bit more flexibility getting, for example, the last year snapshots I think it will get complicated.

I was thinking in adding an intermediary Snapshot for the whole Portfolio and not for the asset that would be simply:

attributes do
  ...
  date :date
  ...
end

relationships do
  has_many :assets, Portfolio.Asset, allow_nil?: false
  has_many :asset_snapshots, Porfolio.AssetSnapshot, allow_nil?: false
end

This way the Snapshot just saves the date and I can easily query there and create some calculations.

In the past I did something similar with a JSONB and I think I could get a similar behaviour here with a :map but I am not sure that’s the Ash-way.

Any insight would be more than welcome! :pray:

Showing Posts 1 to 3

agonzalezro

agonzalezro OP

This is what I was thinking about when I talked about the JSONB:

diff --git a/lib/mcduck/accounts/user.ex b/lib/mcduck/accounts/user.ex
index 1c35cb5..7be6539 100644
--- a/lib/mcduck/accounts/user.ex
+++ b/lib/mcduck/accounts/user.ex
@@ -100,6 +100,7 @@ defmodule Mcduck.Accounts.User do
 
   relationships do
     has_many :assets, Mcduck.Portfolio.Asset
+    has_many :snapshots, Mcduck.Portfolio.Snapshot
   end
 
   identities do
diff --git a/lib/mcduck/portfolio/snapshot.ex b/lib/mcduck/portfolio/snapshot.ex
index 6427ca5..fbced4c 100644
--- a/lib/mcduck/portfolio/snapshot.ex
+++ b/lib/mcduck/portfolio/snapshot.ex
@@ -16,16 +16,32 @@ defmodule Mcduck.Portfolio.Snapshot do
   attributes do
     uuid_primary_key :id
 
-    attribute :amount, :decimal, allow_nil?: false
+    attribute :date, :date, allow_nil?: false
+
+    attribute :asset_snapshots, :map do
+      allow_nil? false
+
+      constraints fields: [
+                    asset_id: [
+                      type: :uuid,
+                      allow_nil?: false
+                    ],
+                    rate_id: [
+                      type: :string,
+                      allow_nil?: false
+                    ],
+                    amount: [
+                      type: :decimal,
+                      allow_nil?: false
+                    ]
+                  ]
+    end
 
     create_timestamp :created_at
     update_timestamp :updated_at
-
-    attribute :rates_id, :uuid
   end
 
   relationships do
-    belongs_to :asset, Mcduck.Portfolio.Asset, allow_nil?: false
-    belongs_to :rate, Mcduck.Money.Rates, allow_nil?: true
+    belongs_to :user, Mcduck.Accounts.User, allow_nil?: false
   end
 end

But I will need to validate that the asset_snapshot maps includes real data as I can’t use the reference. Same when I delete et al. It seems more like a workaround than a real solution.

WDYT?

zachdaniel

zachdaniel

Creator of Ash

I would personally almost always prefer real tables over using maps/embedded to model types. Something that you could do, for example, is have actions on Portfolio that manage its assets, and after any asset is changed you could build and save a full snapshot. You could also snapshot the changing asset as well.

agonzalezro

agonzalezro OP

So do you think that the two resources I have defined make sense for the kind of queries I want to do later?

In case I want to query between two dates, for example one year, I might end up with few hundreds of AssetSnapshots vs few dozens in the case of the Snapshot I shared later. Maybe it’s simply an early optimization and nothing to worry about yet.

— 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
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
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
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
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
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
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews