Ecto virtual fields in a post 'after_load' world

Hey gang!

I’m in the process of updating a practically ancient Phoenix application that made extensive use of after_load to populate virtual fields on Ecto models. I’m pondering how to make use of virtual fields now that after_load is no more.

Another thread recommends making this a view-layer concern, but that doesn’t make a ton of sense to me. The fact that this field isn’t persisted but is derived seems like an implementation detail of my data layer, not a presentation concern. If I’m going that far, I may as well remove this from the Ecto schema entirely and just make this field a function.

In lieu of a callback, I am making a service to load these Ecto schemas and encapsulating all my calls to a Repo there. This is tedious for a couple of reasons:

  • These functions all follow the pattern of “call an identical function on Repo, then call my custom callback”
  • Because I make extensive use of scrivener for pagination, I have separate paginated and unpaginated implementations of these functions, just so I can call custom load callbacks.

Is there a better way to populate virtual fields in Ecto that I’m missing? They seem almost useless these days.

P.S. Is there any hope of bringing after_load back in some fashion? I get the rationale for removing the other callbacks and I think my code is better for having refactored them out, but I sure miss this little function.