jake
banner
jakeprem.com
jake
@jakeprem.com
bsky.app/profile/jake...

This thread has some thoughts about implementing the `has_one_attached` and `has_many_attached` using built in Ecto associations.
jakeprem.com jake @jakeprem.com · Feb 19
Experimenting with building a library with ActiveStorage-like functionality for Ecto.

Convince me this `has_many` usage is a bad idea?

#ElixirLang
September 24, 2025 at 6:11 PM
The basic implementation can be seen in github.com/jakeprem/ect..., look at `EctoStorage.Migrations` and `EctoStorage.Attachments.*`. Right now in `EctoStorage.Schema` I'm just using Ecto's `belongs_to`, `has_one`, and `has_one` with `:through` but a custom association type should be possible too.
GitHub - jakeprem/ecto_storage
Contribute to jakeprem/ecto_storage development by creating an account on GitHub.
github.com
September 24, 2025 at 6:09 PM
This setup should guarantee referential integrity while still allowing for generic attachment handling. It avoids having a join table per schema (no `posts_attachments`) or otherwise having to have extra setup for every schema that needs attachments.
September 24, 2025 at 6:09 PM
Schemas in your app with attachments add a single foreign key to the ledger table per attachment type. e.g. a Post schema could have `cover_image_ledger_id` and `files_ledger_id`. Then any attachments you add have a foreign key to the ledger table as well.
September 24, 2025 at 6:09 PM
Like when you have a `record_type` and `record_id` column to achieve polymorphism the database can't track that. You could deleted the referenced record, or you could break it by changing the type. If you use a setup with foreign keys, the database can guarantee the relational integrity.
September 23, 2025 at 9:43 PM
Short answer is the way ActiveRecord (and ActiveStorage by extension) does polymorphism does not benefit from database reference guarantees. (See hexdocs.pm/ecto/polymor... for "official" Ecto reasoning). I think for wide adoption an Elixir storage solution would want to follow Ecto's patterns too.
Polymorphic associations with many to many — Ecto v3.13.3
hexdocs.pm
September 23, 2025 at 9:41 PM
I started working on a library for this. The key difference from ActiveStorage is using a ledger table rather than record_type/record_id columns for polymorphism. Otherwise I'd like to support roughly all of ActiveStorage. There's an MVP blog example.

github.com/jakeprem/ect...
GitHub - jakeprem/ecto_storage
Contribute to jakeprem/ecto_storage development by creating an account on GitHub.
github.com
September 20, 2025 at 2:29 PM
I’ve been looking for something like this since pglite was first announced!

Super cool!
August 23, 2025 at 12:09 PM
If you don't immediately need strong uptime guarantees why not start with self hosting and point an uptime monitor at it?

Do you have the cluster + your router on a UPS already?
August 18, 2025 at 3:08 AM
This is a great tip! We've added it to dev.exs before too to limit the override to only dev environments.
May 14, 2025 at 9:55 PM
The more conventional approach of `has_many, through:`:

1. Maintains database integrity with foreign keys
2. `Repo.preload(posts, :photos)` includes a query of the ledger table as well as the attachments table
3. `:through` associations are read only
February 19, 2025 at 6:19 PM
Pros of this approach:

1. Both fields still use a foreign key reference in the database
2. `Repo.preload(posts, :photos)` queries the attachments table directly rather than traversing the ledger table.
3. `Ecto.build_assoc/3` and related seem to work.
February 19, 2025 at 6:19 PM
Yep, exactly! It's a scheduling component, so when a user submits a form, it triggers `handle_params` which should reload the data so that whatever events they just saved show up. But I was never actually reloading the events haha
February 9, 2025 at 3:49 AM
I've been using Claude pretty extensively for Elixir work and I'm pretty happy with it. I think it was better than ChatGPT 4o, supported projects, and bigger context windows when I switched.

Biggest advantage though is the background color is more aesthetically pleasing than ChatGPT 😁
January 11, 2025 at 3:43 AM
For most LiveView apps if you create a package.json NodeJS and Bun should be pretty interchangeable right?

I personally use Bun where I can but more developers will probably already have NodeJS installed.
December 24, 2024 at 8:09 PM