Embedded associations in Rails using JSON fields

Learn how to improve the performance and scalability of your Rails applications by using embedded associations with JSON fields. Discover the benefits and limitations of this technique, and learn how to implement it in your own projects.

Arnaud Lachaume
Arnaud Lachaume LinkedIn
• 3 min read
Embedded associations in Rails using JSON fields

When it comes to associations in Rails we are all familiar with the traditional directives such as has_many, belongs_to etc.

But sometimes creating a whole table for an associated model feels like overkill because that associated model will only be used in the context of its parent. What you would like is: be able to embed that association within the parent the same way it can be done with NoSQL databases.

The above becomes even more relevant when the association is actually an ordered list of objects which is always saved as a whole.

Fortunately Rails supports storing attributes as JSON and offers a convenient attributes API which can be leveraged to create nested associations.

The Rails attributes API

The attributes API was shipped in Rails 5.

It allows you to define custom types for database fields such as:

The example above uses a prebuilt type but custom types can also be defined for more complex, object-oriented serialization/deserialization.

Let's see how to do this with a concrete example.

Book and Chapters (first version)

Let's assume we want to create Book model and associate to each book a list of chapters. I'll be assuming we're using Postgres as a database. The same can be done with MySQL.

The Book model

Let's start by creating the migration:

Then let's setup the Book ActiveRecord model:

The model uses the attribute directive to specify that the chapters will be of type Chapter::ListType.

This is the only thing we need to do in the parent model. All the nesting logic will actually be done in the Chapter model.

The Chapter model

The Chapter model will be implemented as a plain ActiveModel::Model. There is no need for ActiveRecord as the list of chapters will be saved on the parent model.

There are three things we need to define on this model:

  • Serializable attributes: required to know what to save in database
  • Equality operator: required to detect changes on the list of chapters
  • Type interface: how to serialize/deserialize our set of chapters

The Chapter model looks like this:

That's all we need to do to define a simple nested association.

Testing our new association

Let's open a Rails console and test our new association. The chapters attribute behaves exactly like any other ActiveRecord attribute.

Alright it works but this implementation is a bit of a one-off. Let's see how we can generalise the implementation.

Book and Chapters (refactored)

It feels a bit overkill to define a nested class called ListType on all the models we wish to embed. From one model to another the only parameter that will change is the actual model class.

Let's extract that nested type class into a dedicated class and parameterize it:

Now let's remove the old code from the Chapter model:

Finally, let's change the attribute declaration in our Book model:

That's all we need. With that refactored approach you can reuse this nested association pattern on various models within your app. All you need to do on associated models is define the attributes and equality methods.

About us

Keypup's SaaS solution allows engineering teams and all software development stakeholders to gain a better understanding of their engineering efforts by combining real-time insights from their development and project management platforms. The solution integrates multiple data sources into a unified database along with a user-friendly dashboard and insights builder interface. Keypup users can customize tried-and-true templates or create their own reports, insights and dashboards to get a full picture of their development operations at a glance, tailored to their specific needs.


Code snippets hosted with ❤ by GitHub Banner designed by starline / Freepik

‍

Ready to Transform Your Analytics?

Join teams already using AI to make data-driven decisions faster than ever.

Most Recent Articles

The Stale Board: Why Jira Is Always Two Days Behind Your Code

The Stale Board: Why Jira Is Always Two Days Behind Your Code

In a typical enterprise, the Jira board trails Git by almost two business days per issue: code is written before tickets reach "In Progress", and tickets stay open days after their pull requests merge. Learn what engineering leaders on Reddit say about stale boards, status-chasing standups and sprint carryover that exists only on paper, and how Keypup MCP compares each issue's workflow timeline with the commits and merges of its linked pull requests to measure the lag and list the tickets to close.

Thomas Williams
The Time Zone Tax: Why Distributed Teams Lose a Day on Every Code Review

The Time Zone Tax: Why Distributed Teams Lose a Day on Every Code Review

In a typical distributed enterprise, a pull request reviewed from another hub waits 4.1 times longer for a first review, and each round of requested changes costs a day instead of a few hours. Learn what engineering leaders on Reddit say about code review across time zones, and how Keypup MCP measures the time zone tax, from the hour a review request is sent to the teams that review locally and the cross-hub pull requests to hand off before logging off.

Stephane Ibos
The Inner Source Tax: Why Pull Requests to Another Team's Repository Wait Days

The Inner Source Tax: Why Pull Requests to Another Team's Repository Wait Days

In a typical enterprise, a pull request sent to another team's repository waits 3.8 times longer for a first review, takes almost five working days to merge and is three times more likely to be abandoned. Learn what engineering leaders on Reddit say about cross-team contributions, and how Keypup MCP measures the inner source tax, from which owning teams are bottlenecks to which cross-team pull requests to unblock today.

Liam Davis