Blog · Articles tagged with "rails" · show all articles »

Testing async ActionMailer jobs with ActiveJob in Minitest

One of the fantastic new libraries that came with Rails 4.2 is ActiveJob - a standard interface to background queues like Sidekiq, Resque or Delayed Job. It lets you write your jobs using the ActiveJob syntax and decide on a background queue later. And if - a year from now - you are not happy with your choice of background queue anymore, you can switch it out without having to rewrite any of the worker code.

But the real power of ActiveJob is that it abstracts the implementation details of the different queuing backends and lets you develop additional tools and libraries around that central API. Any gem can now safely enqueue background jobs for example, without having to know what queue backend the application uses.

An important aspect of background jobs is the ability to test them of course. You want to make sure that a certain piece of code really enqueues a job, and that its the right job with the right parameters!

Read on

How to validate the uniqueness of an attribute across multiple models in Rails

I recently ran into the situation where I had multiple models that each had a identifier field, and each object of each model had to have a unique value for this field.

Now, in my specific situation, I could have used UUIDs, because I was using Postgres and the identifier didn't have any other requirements apart from being unique, but there is a nice generic way to implement uniqueness checks across models in Rails, which is what I chose to use.

Read on

Recalculate counter cache columns in Rails

Rails has this great feature called counter_cache. When you have a has_many/belongs_to relationship between two models, it basically lets you say that you want to save the number of associated objects on the has_many side. So if you have Post has many Comments, you can do post#size and no SQL query is executed, Rails simply just looks at the comments_count field of the post.

And - as with so many things in Rails - the beauty is that it just works out of the box, the way you would expect it to work. When you create or delete Comments, Rails keeps the comments_count fields of all associated Posts up to date. Well, most of the time. I have had the situation a couple of times where I ended up with a negative value which obviously doesn't make sense.

Read on

A nifty service layer for your Rails app

If you have been in the Rails world for a while, you have undoubtedly heard about the discussion around whether, as a Rails app grows in size, it needs an additional "service layer" that contains all the business logic. There have been many blog posts about this topic already so I won't repeat the pros and cons here.

Not sure if I need a service layer or just put it all in /lib

I for one started using services as an experiment in a couple of Rails apps I am working on/maintaining in early 2013 and must say, it has made working on these apps enormously more pleasant! Everything from reasoning about the way the apps work to finding bugs or implementing new features has become much more enjoyable!

Read on

Introducing RubyDocs - Fast and searchable Ruby and Rails docs

Update June 1 2014: RubyDocs is now mentioned as reference documentation on the official Ruby website (look for "Ruby & Rails Searchable API Docs")!

Update May 25 2014: The source code for RubyDocs is now open source under the MIT license and available on Github. If you have any ideas for new features or run into any bugs while using RubyDocs, please open an issue!

RubyDocs was already launched at the end of 2013 but a few days ago I added the most-asked-for missing feature (being able to generate docs for just Ruby or Rails) and noticed that I have never officially introduced the project in a blog post, so here we go.


  • RubyDocs allows to you to generate sdoc docs for any version of Ruby or Rails (or both combined).
  • sdoc is awesome! (it also powers the official Rails API docs)
  •, which used to let you create sdoc docs, has been defunkt for a long time now, which is why I set out to build a replacement.
  • Does anyone know Vladimir Kolesnikov? He owns and I tried to get in touch with him to ask him to forward to but didn't get an answer.
Read on

Load remote Rails links and forms in Bootstrap modals

As a heavy user of Bootstrap modals and Rails, I often want to use remote links or forms in a modal and load the result into the same modal. After doing this in several projects I worked on, I now copy and paste the helper directly into any new project I start that will use modals:

$(document).on 'ajax:success', '.modal [data-remote="true"]', (_, data) ->
      scrollTop: 0
      , 300

The trick is to specify only [data-remote="true"], not a[data-remote="true"] or form[data-remote="true"] so it works with remote links and forms. As you see, I also added a animation that scrolls to modal to the top after inserting the new content.

This helper works with Bootstrap 2.x and 3.x.

Read on

How to drop your Postgres database with Rails 4

Update September 28 2016: There is now a gem that you can include in your app to use the technique detailed below.

When you use Postgres as your database with Rails and try to drop your database using rake db:drop, you might run into this annoying error:

PG::ObjectInUse: ERROR:  database "myapp_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.
: DROP DATABASE IF EXISTS "myapp_development"
Read on

Bootstrap Navbar Helpers for Rails and Middleman

I'm in love with the Middleman static website generator and, after discovering it and falling in love with it, I immediately set out to rewrite all static websites I have up and running with Middleman. Since I'm also a heavy user of Bootstrap I ended up writing the HTML (or rather HAML) for the Bootstrap navbar many times as well. What's a Ruby developer to do? Gemify it!

Read on

CarFinder - Find your closest carsharing car

I am a big fan of the carsharing concept and I have been using the two largest providers DriveNow and car2go regularly here in Berlin since I discovered them a few months ago.

One thing I don't like about them is their websites. While the iOs and Android apps work quite well for finding a car, the websites were obviously designed not only for already registered users who want to quickly find a car, but also for prospective customers, press, investors etc. They contain a lot of clutter and the DriveNow website does not even make an attempt to determine your current location, so you have to zoom and swipe the map manually every time. Especially irritating since you want to reload the map repeatedly sometimes when on the hunt for the closest car.

After getting annoyed with their websites once too often I decided to do something about it.

Read on

Create a cache key from method argument values

In my project ProductWidgets I'm using caching heavily and I happen to be in love with Redis and use it for everything I can. I also really like the concept of key-based cache expiration that 37signals recently wrote about.

Now, in a couple of situations I had to add a lot of variables to the cache key to make sure it caches the right thing. Have a look at this example:

def search(category, query, locale, page, per_page)
  cache_key = [

  Rails.cache.fetch cache_key do
    # Heavy lifting

It occurred to me that in situations like this, where I basically want to cache the whole method based on the arguments, there must be some automated way of doing this where you don't have to add each argument to the cache key. Imagine some other developer comes along and adds an argument to the method and forgets to add it to the cache key! I do not want to be involved in that bug hunt...

Read on