Can a calculation use attributes of a related resource?
E.g., I have
belongs_to :brand, App.Portfolio.Brand
and
calculate :tagline, :string, expr(brand.name <> " β " <> name)
The result:
18 Replies
It cannot π
To do that, you'd define a
first
type aggregate, and refer to that
Thank you, clear! Then, in
iex
, I do App.Portfolio.load(f, :tagline)
, where f
is a record of type App.Portfolio.Family
.
(For reference, for others.)
Thanks, @Zach Daniel!my pleasure
And is it possible to set an identity on an aggregate? (Probably not, huh?)
Correct, that is not possible
Well...it might be?
but it won't create a unique index when making migrations, for example
What would you want the identity for?
I'm rewriting a Product Data Management thingy I built last year in Python/FastAPI, now with Ash, and instead of copying text between tables, I build my database schema properly.
So, a sales :item has a unique 7-digit code.
The first 3 digits come from the :code field of the product :category it belongs to.
The last 4 digits come from the :code field of the product :variant it is linked with.
Within App.Portfolio.Item I define two aggregates (:category_code, :variant_code) as you explained above.
Then I was thinking of defining a calculation that concatenates the two aggregates into an :item_code calculated field.
The idea is to be able to lookup an App.Portfolio.Item by this :item_code.
Possible?
Yes, definitely possible π
I think the identity may work for you in that case, actually
But you also don't necessarily need it for look ups
Ah, so it works on the calculation
You can do:
and things like that. There are various ways to do "get" style actions
with or without a corresponding identity
I also noticed that
Ash.Resource.Relationships.BelongsTo has both attribute_writable? and writable?
whereas
Ash.Resource.Relationships.HasOne only has writable?
Yeah, so belongs to relationships add an attribute, i.e
belongs_to :user
adds user_id
You can control the writability of both the relationship and the generated attribute.Yep, got it -- but HasOne doesn't have the
attribute_writable?
optionCorrect, because for has_one, the attribute that changes is on the other resource.
Aha... I see what you mean.
To associate A with B, if A has_one B, can you set the relationship field during :create / with an :update, or do you write A's :id into B's a_id ?
Youβd write Aβs id into B.
has_one
is actually not very common in the wild.
Itβs exactly the same as has_many
except the destination is unique on the destination attribute.Great, it works
I was trying with :update by passing the record directly and it didn't work
@Zach Daniel , actually, I see that I cannot pass the record directly to the relationship attribute. After
load
, I get nil
on the attribute value.
Is my understanding correct that :create and :update can only set attributes that are not relationships?By default they accept all public, writable attributes.
I think I lean too much on my Django ORM experience, and this confuses me π