AE
Ash Elixirβ€’2y ago
adonig

Error while building reference

I'm trying to use the attributes of a relationship in a calculation but I get a runtime error. I attached my code and the error as a screenshot because Discord somehow prevents me from creating posts with more than 2000 characters.
No description
9 Replies
barnabasj
barnabasjβ€’2y ago
I'm not sure, but maybe adding the load option will help https://ash-hq.org/docs/dsl/ash-resource#calculations-calculate-load load: :vendor otherwise I'd need to dig a little deeper
Ash HQ
Ash.Resource
View the documentation for Ash.Resource on Ash HQ.
adonig
adonigOPβ€’2y ago
I previously already unsuccessfully tried load: [:vendor] Now I added load: :vendor, recompiled and tried it again but I still get the same error. I already had a look at the ash_postgres source code and it looks like vendor lacks a binding, but I'm not skilled enough to debug it there.
No description
barnabasj
barnabasjβ€’2y ago
load: [:vendor] would have been the correct syntax, my bad. I have to take a closer look later, or maybe Zach swings by in the meantime and can just answer it πŸ˜‰
ZachDaniel
ZachDanielβ€’2y ago
You can’t reference related resources in a calculation We should give it a better error You can, however, refer to aggregates which reference related resources Lemme move to my laptop to show you what it would oook like, once sec
adonig
adonigOPβ€’2y ago
Thank you very much for the help! Btw watched the Elixir Conf talk on YouTube and I'm absolutely hyped about the future of Ash πŸ‘
ZachDaniel
ZachDanielβ€’2y ago
calculations do
calculate :name, expr(vendor_name <> " " <> model)
end

...

aggregates do
first :vendor_name, [:vendor], :name
end
calculations do
calculate :name, expr(vendor_name <> " " <> model)
end

...

aggregates do
first :vendor_name, [:vendor], :name
end
That is the basic form of using an aggregate to get a single value from a related entity. Honestly...I can make that not necessary, I just haven't done it yet πŸ˜† I can make calculate :name, expr(vendor.name <> " " <> model) work
barnabasj
barnabasjβ€’2y ago
Makes sense, it could potentially work for has_one, belongs_to relationships, but otherwise it's not clear which resource entity the name should come from
ZachDaniel
ZachDanielβ€’2y ago
Exactly we'd raise an error if any relationship had a :many cardinality The other way is with an "inline aggregate"
calculate :name, expr(first(vendor, field: :name) <> " " <> model)
calculate :name, expr(first(vendor, field: :name) <> " " <> model)
adonig
adonigOPβ€’2y ago
Thank you very much! It works like a charm. I will probably keep the explicity aggregate because it might come handy later. Awesome πŸ’―
No description

Did you find this page helpful?