haubie

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!

Showing Posts 1 to 7

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 #1
haubie

haubie OP

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

kamaroly

Can a metadata attribute be used in AshPhoenix Form?

zachdaniel

zachdaniel

Creator of Ash

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

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:


%Resource{
    Id: UUID STRING,
    title: "just a test",
    custom_fields: [
      %{
   
        "id" => Ash.UUID.generate(),
        "type" => "text",
        "value" => "John",
        "name" => "name",
        "options" => [],
        "description" => "Description of the field",
        "is_required" => false,
        "default_value" => "New Name",
        "order" => 1,
        "placeholder" => "Field placeholder",
        "label" => "Field Name"
      }
    ]
  }
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

kamaroly

kamaroly

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews