fireproofsocks
I’m wondering the best way to solve this particular problem: I have a schema for file uploads (think of an asset manager). The database record stores the file size, the file name, and its location. I also have a Phoenix endpoint where one can stream that file, e.g. for use as a src in an <img> tag. The route is something like /download/:id.
My question is: how to add a field to the Ecto Schema where I can populate the download url? The schema struct already knows the ID of a record, so the only thing I need to do is to build the URL using one of the Phoenix route helpers. Would this require a custom type?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
amnu3387
I think virtual fields are a good use case for it,
For instance:
Personally, if the full url is the only thing that makes sense for the client(s), I replace the field itself with the valid url, if the client needs/can use the field value for something, I define a virtual field and then populate that instead.
al2o3cr
Do you need the data on the schema itself, or is it sufficient to include the URL when the struct is serialized to JSON? In the latter case, @amnu3387’s solution will do exactly what you need.
In the former case, things are more complicated - invoking route helpers from inside schemas will mean a circular dependency between the data layer and the Phoenix layer.
fireproofsocks
That makes sense – is
:avatara virtual field in that example? I haven’t tested that, but is the virtual field even necessary if you are just putting a field into a map?And I guess for thoroughness, I would love to see an example of how to add something to the schema. E.g. the class accessor example of having a
first_nameandlast_namefield in the database, and then having a “virtual” field forfull_namethat concatenates the two together.amnu3387
I completely misread what you’re wanting to do - that is not, indeed, useful for creating routes for use inside the app instead for providing the final link outside of it (spa, wtv). Isn’t there a helper to build urls already?
Regarding the virtual field, it was just an example, sometimes it makes sense other times not?
dimitarvp
Doesn’t arc_ecto do what you need?
fireproofsocks
I want to know how to create accessors in Ecto – the exact use-case is secondary. The “full_name” field is a better example, perhaps.
fireproofsocks
Yes, there’s a helper for that use case, but as @al2o3cr mentioned, that would create a circular dependency. The primary concept I’m trying to figure out is how to introduce a calculated field on read so that the rest of the app won’t know or care that its value did not come directly from the database.
fireproofsocks
For a GraphQL endpoint, it was easy enough to add in the field in the resolver…
lindem
I usually have a function in my schema’s defmodule that fills virtual fields for that purpose (full name field example), which is called from functions in the context after the actual
Repointeraction.As far as I know, there is no way to have something that looks like you do
map.propbut that doesgetter_for_prop(map)instead (like, for instance, ember computed properties or more generally ES6 getters and setters).It looks like this (off the top of my head):
(You can pipe a list of schema instances through
Enum.map())jgb-solutions
Hey! How do you deal with virtual field with relationship in Absinthe then? Say you want to fetch an
Artistwith theirTracksbut each track as a virtual field,poster_url, to fetch to url of the poster column. What’s the easiest way to do that?