# Virtual attributes mirroring Ecto's virtual fields

**URL:** <https://forum.elixirforum.com/t/virtual-attributes-mirroring-ectos-virtual-fields/58608>\
**Category:** Questions\
**Tags:** virtual-fields, ash\
**Created:** [October 1, 2023, 11:49am UTC](https://forum.elixirforum.com/t/virtual-attributes-mirroring-ectos-virtual-fields/58608 "2023-10-01T11:49:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [October 1, 2023, 12:54pm UTC](https://forum.elixirforum.com/t/virtual-attributes-mirroring-ectos-virtual-fields/58608/2 "2023-10-01T12:54:27Z")

</div>

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:

```elixir
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:

```elixir
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)

```elixir
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!

---

_[View the full topic](https://forum.elixirforum.com/t/virtual-attributes-mirroring-ectos-virtual-fields/58608)._
