Building a Game Player with ChatGPT and Rails - Part 2
DougDoug is a Twitch streamer (and programmer!) who recently did a fun stream making ChatGPT play a children’s point-and-click adventure game. It’s a long bu...
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.
DougDoug is a Twitch streamer (and programmer!) who recently did a fun stream making ChatGPT play a children’s point-and-click adventure game. It’s a long bu...
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...
Watching Twitch streamers and their highlight videos is a guilty pleasure of mine. DougDoug does a lot of streams where the main idea utilizes AI, typically ...
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,...
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...
I’ve been on vacation this week, which means two things:
Our team recently came across a fun little quirk in the as_json method of an ActiveRecord model.
Jurassic World Dominion just came in movie theaters. It is absolutely terrible, and I love it.
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...