haubie
Question to the Ash gurus: in the same way Ecto schemas can have virtual fields (e.g. by passing the virtual: true option, so that those fields are part of the schema/struct but not persisted to the database), does Ash have a similar concept for attributes? e.g.
attributes do
# Core data, persisted
attribute :title, :string
# Virtual attribute, not persisted
attribute :placeholder, :string, virtual: true
end
I’m very much new to Ash and learning by migrating a current project to use it (the Ash livebook tutorial is a fantastic intro, well done whoever created that!). By doing this, the benefits of the framework are becoming more obvious to me.
I’ve only run into one stumbling block which was a situation where I was using Ecto’s virtual fields. I’ve taken a look at the documentation and source code on Ash attributes and Ash.Types, but couldn’t see any obvious equivalent.
Thank you!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 7 of 7 Posts
zachdaniel
Ash has three concepts that map to the concept of a virtual field. Often, virtual fields exist to simulate one of these three things
Arguments
Arguments are action-specific inputs. For example:
In the example above there is no “password” attribute, but we have two arguments that we use to ultimately write to that attribute.
Calculations
Another thing virtual fields are used for in ecto is storing computed values. In Ash we use calculations for that. For example:
Action-specific metadata
Finally, we have action-specific metadata. An example might be returning an authentication token from a sign in action (something
ash_authenticationdoes)Then the result of that will be in
record.__metadata__.token.Hope that helps!
haubie
Great, thanks very much @zachdaniel, appreciate the thorough answer!
I was looking in the wrong place, it hadn’t clicked with me that there were some action-specific approaches rather than defining them as part of the attribute block.
kamaroly
Can a metadata attribute be used in AshPhoenix Form?
zachdaniel
Hmm…not really. You could use the value of a metadata field, with something like
value={form.source.data.__metadata__[:metadata_field]}, and then you’d have an argument w/ the same name as that metadata that sets the metadata accordingly. But I’m not sure why you’d want to do that.kamaroly
I have added custom_fields which has an array of multiple fields. I want to dynamically add each field to the form so can use it like <.input field={@form[:custom_field_id]} />
Here’s the structure of the data:
zachdaniel
Where do your custom field values get stored? I assume in a map-typed attribute in the resource? And then to accept them as input you’d need to accept a map of custom field values too? So then you could do things like (just as a potential example)
<.input name={@form[:field].name <> “[field_1]”} id={@form[:field].id} value={@form[:field].value}/>And you could probably make that into its own
custom_field_inputcomponentkamaroly
Correct. They are stored in an
{:array, :map}attribute.I wanted to make a custom_field the like an attribute on a resource, but your proposal works for now. Thanks for the proposal.