Skip to content

Instantly share code, notes, and snippets.

@thanos
Created October 13, 2024 17:05
Show Gist options
  • Save thanos/8f5894a65e049c9257ea133bd817e5d9 to your computer and use it in GitHub Desktop.
Save thanos/8f5894a65e049c9257ea133bd817e5d9 to your computer and use it in GitHub Desktop.
A select merge example for populating a virtual field
defmodule Answer do
use Ecto.Schema
schema "answer" do
field :word, :string
field :length, :integer, virtual: true
end
end
Repo.insert(%Answer{word: "SWANS"})
Repo.one(
from answer in Answer,
select_merge: %{length: fragment("length(word)")}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment