Ruby on Rails — Handling Data (part 1)

Paulo Santos
Deemaze Writing Wall
6 min readFeb 26, 2018

Hello, you! It’s 2018 and you’re about to start a web project. You decided to go with Ruby on Rails, and you would really appreciate some tips about the best gems, tools and patterns out there? Then keep reading, this series of articles might be just what you were waiting for. 😅

Rails is a great framework for web development. It has a huge community, fantastic built-in features and keeps improving and keeping up with the industry standards to provide the latest and the greatest to web developers.

However, the aim of this series of articles is not to teach you Ruby on Rails — for that you should definitely start here. Instead, I want to provide you some guidance about tools you can use to help yourself on those rainy developer days.

Using Rails like a pro!

I have already used most of these tools over the last few years I’ve developed with Rails, and others I gathered from discussing with some enlightened fellas.

Handling data on Rails

Let’s get this started, then. The first part will be about the core of all apps: data. You’ll probably deal with all kinds of data, you’ll want to manipulate it, control who accesses what, and all of this while keeping your code tidy. Let’s check out some gems to help you with that. 😱

Authentication 🔑

Devise enables your app with super skills for user authentication, with all kinds of features — email confirmation, password recoveries, keeping up multiple user sessions simultaneously, account locking and unlocking, virtually anything you can imagine in a modern web app. Also what’s great is that all of this is configurable, so you can adjust it to your needs and keep the remaining features out of your way. Great!

Clearance provides you a solid alternative to Devise if you don’t need to be all fancy feature-wise. If you want to keep things simple for a basic email/password authentication setup, you should definitely give it a try.

Need to integrate an external service authentication into your app? Say no more, OmniAuth to the rescue! You’ll find dozens (if not hundreds! 🤔) of pre-built providers for almost all famous web apps out there. Also, there are many abstract strategies for you to roll out your own implementation while having a nice basis to start with.

Resources authorization 👮‍

CanCanCan allows you to easily set up access rules for your data. After that, you’ll have nice helpers to use in your controllers/views to conditionally provide access to certain features of your app. Neat!

Pundit does very much the same as CanCanCan, although by default it encourages you to be a bit more organized with separate policy (rule) files. You’ll also get nice helpers for authorizing resources, so I’ll call it a tie and tell you it’s a matter of taste. But hey, isn’t it always better when you need to choose than when you have nothing to choose from? 😅

Location 🗺

Geocoder will allow you to handle whatever you need location-wise. It provides an easy integration with several geocoding APIs, allows you to perform location queries on your models, you can even use multiple API services at once! If you’ll be dealing with locations, this is probably a must for your app.

Internationalization 🌎

Ruby on Rails is great with I18n and for your data you will find in Globalize a great friend as well! It will allow you to store translations for your data for any locale you want, and to pull that data automagically from your DB according to the default locale. Cool, huh? 😎

Auditing/versioning 🕵🏻‍

PaperTrail is a tool that allows you to keep track of all changes in your data. By adding it to your models, you’ll be able to travel back in time and see all their previous versions, who performed the changes and even retrieve deleted data.

Audited is a bit more focused on auditing the actions a certain user performs on your data. It works pretty similarly to PaperTrail, so in the end, it’s almost a matter of taste. Once again, both are great tools and if you have the chance to play with both you’ll discover just that.

Soft deleting 🗑♻️

Sometimes you don’t need to track all changes in your models, but you still want to easily undo deletions. Thanks to Paranoia, you’ll be able to do just that. If your model has no relationships whatsoever you’re good to go, however, keep in mind that you might get into trouble when your model has associations that you don’t intend to soft delete.

As per Paranoia’s README, you may find a better approach in Discard when it comes to marking records as inactive. It does fewer assumptions and tries to be less magical, while also providing you with more predictable results. Looks promising, you should definitely keep an eye on it! 👍

Enumeration 🔢

Ever wanted a quick and easy way to specify and enforce a set of possible values to one of your model attributes? It’s not that hard of a task to achieve on your own, but Enumerize will save you some time. It offers a clean integration for validations, support for some frontend libraries out of the box and great support for I18n.

Files 📂

Paperclip offers a simple integration for uploading and saving files in your app. It is a perfect match for simple apps, as it is easy to configure and use. *

CarrierWave is a much more versatile solution for file uploads. If you need to perform some processing on your files or you like to control things according to your taste, you should probably give it a try!

A note on two other uploaders that you might want to take a look. Refile has nice built-in support for direct uploads to Amazon S3 and was built by the author of CarrierWave as an attempt to fix some of its downsides. On the other hand, Shrine also supports pre-signed S3 uploads directly, and if you’re dealing with large files it seems to go easier on your server memory usage than CarrierWave.
In the end just remember: the smartest choice is the one that fits your own requirements the best way.

Update: Paperclip is now marked as deprecated in favor of the new Active Storage solution bundled within Rails from version 5.2 onwards. Thanks to Tony Dehnke for pointing that out in the comments! Probably that will be the recommended way of handling file storage from now on. 👌

JSON serialization { 📦 }

ActiveModelSerializers is probably the most used library in this field and provides you with a relatively simple setup for serializing your resources into custom JSON. You can define multiple serializers for the same resource with different contents, serialize associated records, use different output formats like attributes list serialization or JSON API and much more.

If you’ll need to be fully compliant with the JSON API spec you may want to consider JSONAPI::Resources. The serialization setup is straightforward, and in addition, you can have your controllers accepting requests in JSON API format to perform CRUD operations. 🎉

Recently, Netflix released their own JSON API serialization mechanism, Fast JSON API. As its name suggests, they’re aiming at being fast 😅 and so far the results are promising, with a 25x quicker solution when compared with ActiveModelSerializers, as per their benchmark. Well done! 👏

Wrap up

Ugh, we’re done for now! It was great to show you some of the tools we’ve been using or intend to use in our future projects. Let us know your opinion about the above gems, and also tell us about others you are using in your Rails projects to manipulate data. 😇

Glad you made it through!

Want to know more? Check out the next article on this series about the frontend capabilities of Rails, and how it plays out with existing frontend frameworks.

Deemaze Software is a digital agency developing products for web and mobile. Get in touch with us on Twitter, Facebook or Instagram. Feel free to hit us up with any questions you have about our use of Ruby on Rails!

See you soon. 🖖

--

--