haubie

haubie

Virtual attributes mirroring Ecto's virtual fields

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!

Most Liked

zachdaniel

zachdaniel

Creator of Ash

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:

create :register do
  accept [:email]
  argument :password, :string, allow_nil?: false
  argument :confirm_password, :string, allow_nil?: false
  validate confirm(:password, :confirm_password)
  change EncryptPassword
end

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:

calculations do
  calculate :full_name, :string, expr(first_name <> " " <> last_name)
end

Action-specific metadata

Finally, we have action-specific metadata. An example might be returning an authentication token from a sign in action (something ash_authentication does)

read :sign_in do
  get? true
  # this just declares that some metadata will be placed on the result called `token`
  metadata :token, :string, allow_nil?: false

  ... logic for signing in.
  prepare CreateAndSetToken # some logic to create and set that token metadata
end

Then the result of that will be in record.__metadata__.token.

Hope that helps!

11
Post #2
zachdaniel

zachdaniel

Creator of Ash

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_input component

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement