Jack Tang
Jack Tang

Reputation: 181

How to define virtual attribute in Ecto model

I'm not very clean about virtual attribute in Ecto model, Is it only mapped to the query result?

Upvotes: 8

Views: 3076

Answers (1)

Fabi755
Fabi755

Reputation: 1514

See at documentation:

:virtual - When true, the field is not persisted to the database.

Virtual fields exists temporary in the Schema and were not saved in the database. This is helpful for local processes and validations.

Example: An password confirmation field.

schema "users" do
  field :username, :string
  field :password, :string
  field :password_confirmation, :string, virtual: true

  timestamps
end

I hope this helps.

Upvotes: 16

Related Questions