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”
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”
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):
client = HTTPClient.new
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.
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)”
Making use of REST APIs is a commonplace task for many a software developer. When working with Ruby there are quite a few RubyGems that you can use to aid you in this endeavour. Faraday, httpclient and HTTParty are examples just to name a few. If you however do not want to add dependencies to your project you can also make us of Ruby’s standard library, which is what I will be going through in this article.
Continue reading “Consuming a REST API with the Ruby standard library”