Dumping Data as JSON and Field Deprecation in Active Record

Our team recently came across a fun little quirk in the as_json method of an ActiveRecord model.

We’re in the process of deprecating a set of fields, and had set up the following:

class OurThing < ApplicationRecord
  belongs_to :our_other_thing
  ...
  def our_old_field
    raise "`our_old_field` is now deprecated"
  end

  def our_old_field=(newval)
    raise "`our_old_field` is now deprecated"
  end

  def as_json(options = {})
    options[:except] << :our_old_field
    super(options)
  end
  ...
end

This took care of almost everything, as this field wasn’t intentionally being used anywhere. The as_json method extension gives a nice way to ensure that this field is never returned in that payload.

This fell apart when including OurThing as part of another model:

our_other_thing.as_json(include: :our_thing) 

This bypasses the exceptions we built in to OurThing#as_json and attempts to return our_old_field as part of the payload, which in turn raises our deprecation error (thankfully we caught this in our specs).

The solution is to explicitly-exclude our_old_field in these cases:

our_other_thing.as_json(include: { our_thing: { except: :our_old_field } })

Might be a bit onorous to do this everywhere, but hopefully cases like this (which are essentially a table dump) are rare - it certainly drew our attention to places in our code where we need to be more intentional.

2023

Example: Using after_reply with Puma

5 minute read

Puma has a pretty interesting feature called after_reply - if there’s a potentially costly operation that’s not on the critical path to responding to a consu...

ChatGPT

2 minute read

ChatGPT, created by OpenAI, is an amazing tool that’s helpful in refining rote tasks, and will eventually become as commonplace as spellcheck tools. However,...

Unhappy Paths Matter

5 minute read

Most product, design, and engineering folk are well-aware of their app’s Happy Paths - that is, under all the right circumstances, the imagined optimal set o...

Back to Top ↑

2022

Capturing Redirect Metrics in Rails

2 minute read

When you’re cleaning up a monolith Rails app, it’s essential to have usage metrics to know what pieces of code are safe to remove. Tools like Datadog APM and...

Back to Top ↑