Sheharyar Naseer

Association Defaults in Ecto

Updated: Open Article

I just published a new post on the Slab blog, covering a little-known feature in Ecto!

To give a quick overview, Ecto lets you specify default fields for associations in the schema definitions, but what’s super-interesting is that the :default option can also accept an MFA tuple, allowing you to execute custom logic to build the aossication.

What’s even better is that this works with pivot tables (:through associations) as well! This has wide range of use-cases like multi-tenancy, security, reliability, database sharding, and more.

Read the article →


The post got featured in various newsletters:

I also described our use-case in a little more detail on and a Twitter thread: