Rails: ActiveRecord descendants in development mode

Single table inheritance (often referenced to as STI) is a feature offered by Rails’s ActiveRecord (which is documented here) that allows you to subclass models to store data for multiple similar models in one database table. This can be a very useful feature under the right circumstances.

When working with Rails in development mode classes are not preloaded (unlike is done in production mode). If you use the descendants method to get a list of the subtypes of your model (to for example display them or to populate a select box) it is possible that sometimes an empty or partial list is returned rather than a list of all the defined subtypes (unless you have actually used one of them so far).

If you are working in a team this can be an especially devious little problem that can linger for a long time. Different developers may have different sets of data in their development system. Loading a list of STI models will load the subtype classes making them available in the application from that point on. If the database however does not contain records for the subtype’s class and you are not directly referencing it you will not see it. This can then result in one developer experiencing issues while the another does not.

In this article I will demonstrate the issue and offer a solution that you can implement.

Continue reading “Rails: ActiveRecord descendants in development mode”

Prepend to an existing rake task

Rake is a commonly used tool in Ruby projects to provide command line tasks, Rails comes with a number of rake tasks included for example. It is quite well documented how to write rake tasks, a while ago however I needed to prepend something to an existing rake task. One way to achieve this is to write a new rake task that executes the code that you want to execute and then calls the existing rake task. The (perhaps obvious) downside to this method is that you have to execute another rake task than you usually would.

It turns out that there is another way, you can use the enhance method on an existing rake task. In this article I will briefly explore how you can use this method.

Continue reading “Prepend to an existing rake task”

Splitting commits using git rebase

We have all been there before: you’re working on a big feature branch, committing all over the place and then at some point you need to extract something out to merge it back to the main (or develop) branch. My usual to method is to split off a new branch and then use git rebase to keep only the commits of the parts that I want to extract out and merge that back after which I then rebase my feature branch on the target branch again.

But what if you find that some commit halfway through the changeset needs to be split up? That’s what I will be diving into in this article.

Continue reading “Splitting commits using git rebase”

Using bundler inline

A little while ago someone showed me a pretty cool feature of Bundler that I didn’t know about: you can use it inline in a Ruby script without having to create a separate Gemfile. This is very convenient for writing little utility scripts.

To use bundler this way you start with a require bundler/inline followed by a gemfile block that contains the content that you would have put in the Gemfile if you would have had to create one. When you then execute the script with Ruby Bundler will first install the required gems (if needed) before executing your code.

Here’s an example that uses the HTTPClient gem to check your external IP address (using the same endpoint I made for my article on obtaining your external IP address from the command-line):

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'httpclient'
end

client = HTTPClient.new
puts client.get('https://ip.5ec.nl').body

If you execute this script for the first time it will pause a moment to install the HTTPClient gem, the next time you execute it will work without delay.

This and other features of Bundler are well documented on the official Bundler documentation pages, it’s well worth having a look around there.

If you have feedback on this article or have questions based on its contents then feel free to reach out to me on Twitter or through e-mail.

Capybara with Selenium and Vagrant (without Rails)

Once upon a time I wrote a blog post about using Capybara with Selenium and Vagrant (which you can still find on the previous incarnation of my blog here) … things have changed a bit since then however: it got easier to run headless browser tests for example (no longer requiring an Xvfb setup) and nowadays Rails even ships with Capybara by default for its systems tests taking away a lot of the complexity of setting it up for developers using the framework.

Capybara with Selenium is still an amazingly good combination for automated testing of web applications in a real browser, so in this article I will revisit running automated browser tests with it on a Vagrant box with the same perhaps somewhat unimaginative little Sinatara application as I did back in 2012.

Continue reading “Capybara with Selenium and Vagrant (without Rails)”

Using OpenSSL to verify downloads

Hashes are commonly offered by download pages for you to verify the integrity of your downloaded files. I typically verify these hashes using OpenSSL which supports many types of hashing algorithms and is available on many unix-like systems (and can often be installed on them easily enough if it is not yet present).

The basic method for verification works as follows: after downloading a file enter the command openssl {hashalgorithm} {/path/to/downloaded.file} to calculate a hash based on the file’s contents. If the hash shown matches the hash on the download page the file is as intended.

Continue reading “Using OpenSSL to verify downloads”

Committing parts of your changes via the git command-line interface

If you have ever used made extensive use of GUI interfaces for git you may have come across the ability to stage and commit only parts of your changes to files rather than committing everything that you changed in said files at once. Recently I learned that the command-line interface to git supports the same feature using the -p flag when staging or even committing files which is what I briefly want to go through in this article.

Continue reading “Committing parts of your changes via the git command-line interface”

Scripting tmux

I spend quite a bit of time on the shell, more often than not inside a tmux session. For those of you not familiar with tmux, it is a terminal multiplexer (akin to GNU screen) allowing you to split your terminal into multiple windows and panes. Its a tool that offers man conveniences such as being able to start a session on a server, detach from it and to come back to later (in a different SSH session) or even having multiple people sharing a single session without having to physically be in the same place.

When developing with Ruby I tend to program using Vim in a tmux session. In my setup I usually have a window per project which has three panes: one for Vim, a second to run processes in (such as a rails web server) and a third as a console to run ad-hoc commands or an application console. With my configuration this setup looks something like the below screenshot:

While you can easily create the above panes with a few keystrokes, you can also script tmux to setup this up for you with a single key combination. This is what I will be diving into in this article.

Continue reading “Scripting tmux”

Flushing your DNS cache on Ubuntu 20.04

Often enough I run into this little problem: I make a DNS change and want to see the results. When I check for the changes though it looks as if nothing has changed, and sometimes this is caused by my system’s DNS cache.

Clearing the cache on Ubuntu 20.04 can be done with the following command: sudo systemd-resolve --flush-caches, you can verify that the caches have been emptied by following that up with the command systemd-resolve --statistics which then gives an output like shown below:

DNSSEC supported by current servers: yes

Transactions               
Current Transactions: 0    
  Total Transactions: 21870
                           
Cache                      
  Current Cache Size: 0    
          Cache Hits: 6722 
        Cache Misses: 15158
                           
DNSSEC Verdicts            
              Secure: 7783 
            Insecure: 21703
               Bogus: 0    
       Indeterminate: 0    

The text and numbers will vary per setup, the “Current Cache Size” should however display a value of 0 to indicate your cache has been cleard.

If you are running dnsmasq next to the standard systemd-resolve like I do, restarting dnsmasq (with sudo systemctl restart dnsmasq) will ensure its caches are cleared to (otherwise you may still end up getting cached results).

And that’s all that I have for this very short post. If you have feedback on this article or have questions based on its contents then feel free to reach out to me on Twitter or through e-mail.

Obtaining your external IP address from the command-line

Sometimes you need to know what your external IP address is, for example when you need to make use of a protected service where they need to (temporarily) add your IP address to their firewall’s allow list to allow you to connect to said service.

If you have a broadband connection at home it is possible that you have a fixed external IP address and that you already know what it is. If you are however visiting elsewhere you likely do not know what it is, a quick solution in such cases is to open your browser and use a site such as https://www.whatismyip.com/ to see what your external IP address is. But what if you need to know this on a Unix-like server that only offers a command-line interface (or if you simply prefer to use your shell)?

Continue reading “Obtaining your external IP address from the command-line”