Extending ActiveRecord with custom types
In the last article, we saw how to use the ActiveRecord Attributes API to make Ruby on Rails models more communicative. The two explicit benefits of doing so from that article were a more discoverable data model and a uniform way of defining different types of attributes. The article also hinted at using richer data types to model attributes. That is the topic of this article.
Use the Attributes API to make your Rails models more communicative
ActiveRecord is the powerful object-relational mapper at the heart of Ruby on Rails. By default, it gives you tools to quickly and easily create new database tables and map them to domain models. It follows the ethos of “convention over configuration” that David Heinemeier Hansson coined with the release of Rails. As such, with little application code, you get a powerful, database-backed model that Just Works™.
Solving the right problem
Recently, I received a bug report on one of my open source projects. The bug report came from my co-maintainer on Hashie, dB, who I hold as an exemplary asker of questions. I quickly read through the issue and the background information on his issue and quickly sent him a reply that his suggestion sounded reasonable and that I would accept a contribution adding that behavior. I was attempting to be more like him and act as a facilitator, which is an attribute that I admire in him. However, I did a poor job of this task.
Using ox-hugo without duplicating content in the repository
My new blogging setup mashes up Org mode with Hugo using the ox-hugo library. This is part of my attempt to bring more of my work product into the single format of Org mode. Hugo has rudimentary support for Org mode through go-org, but it admittedly only tries to cover the 80/20 use case of Org mode. Since I intend to use Org mode for everything, the chances are high that I will end up with something incompatible, thus requiring me to work around the problem. I didn’t want to do that, so I stuck with the Hugo export library.
Provisioning a Valheim server with Terraform
My friends and I decided we were going to give Valheim, an early-access survival game, a try. Since many of us are parents now and we’re in a socially distancing world, colocating for an evening of fun was out of the question. I decided to use the event as an excuse to practice my DevOps skills.
Duplicate cookies in Ruby on Rails
Ruby on Rails has an easy-to-use cookie store for managing state between requests. It has affordances for storing clear-text values, tamper-proof signed values, and encrypted values. I previously showed how you can make good use of encrypted values for handling ActionCable authentication. However, there is a case where you might end up with duplicate cookies in Ruby on Rails applications.
3 Ways to Make Arel a Joy
Arel, the SQL syntax tree constructor library backing ActiveRecord, allows you to express your SQL queries without the use of fragments. ActiveRecord uses it internally to represent a variety of queries. It does this transparently using the traditional
where(key: value)syntax that we’ve seen since the early days of Rails.
Null-based ordering in ActiveRecord
When designing your domain model for an application, the natural solution might end up using a
nilto model the absence of a value. In these cases, you might need to reach for null-based ordering within your database. When you’re writing a Ruby on Rails application, you’re most likely going to use ActiveRecord as the interface to your database. While ActiveRecord supports most queries out-of-the-box, sometimes you have to go off the beaten path.
Memoization in Ruby using three different patterns
Memoization is the process of performing an expensive calculation — a lookup from a database, a long-running function, etc. — and then caching its value to prevent the expensive action from running again. In computer science jargon, it trades space complexity for time complexity; that is, instead of releasing the calculated value for the garbage collector, it stores the value, thus increasing the space requirements of the object, to save on the time it takes to calculate the value more than once.
Implementing account impersonation in ActionCable
Account impersonation is when you allow accounts (usually privileged accounts, e.g. support staff or developers) to operate your Rails application as if they are the owner of another account entirely. It’s a helpful feature to have when diagnosing issues that your customers write in about. There are plenty of gems that implement impersonation for your Rails controllers, but I wasn’t able to turn up any suggestions for implementing this for ActionCable.
I'm a Rubyist by trade and maintain Hashie and KSUID for Ruby. I am working to better the IndieWeb experience in Ruby, in particular for the Bridgetown static site generator, which I use to host this website.