Nested Single Table Inheritance doesn’t work well. Here’s what you must know to make it work or work around it.

Image for post
Image for post

Some context for illustration

I recently stumbled across the following scenario.

Initial specifications: a project owner creates a project and donors can contribute any amount of money to that project.

Later, a little change was made to the specifications: a donor may either be a natural person (an individual human) or a legal person (a corporation or any other kind of legal entity).

Since both are donors and will share some significant amount of logic, it seems obvious that they are both a specialization of User::Donor


Image for post
Image for post

Do you feel lost with all the changes related to assets and Javascript? Npm, Babel, ES6, Yarn, Webpack, Webpacker, Sprockets, do they all look like complete strangers to you?

If you need a quick, easy to understand topo on how this whole Javascript ecosystem works in a Rails 6 application, this article is what you’re looking for.

I’ll end up this article with a step-by-step section explaining how to add Bootstrap 4 and FontAwesome 5 to a Rails 6 project.

NPM

NPM is a Javascript package manager (NodeJS modules to be precise). It is the Rubygems of Javascript world.

npm install…


Image for post
Image for post

Oftentimes we need contextual validation in ActiveRecord models and find no ideal way of doing so. This article exposes an elegant, lightweight, dependency-free solution.

First, let’s have in mind two case studies that perfectly illustrate this matter:

  • State machines: you need to run different validations depending on the current state of the record.
  • Progressive completion: one may fill the form at step n only if all previous steps are completed.

What tools ActiveRecord provides to deal with this?

  • Create an STI and mutate the type of your record.
    — Upside: you can run “always required” validations and “type-specific” validations.
    — Downside: you cannot run “type-specific” validations for multiple types all at once, you would have to mutate your object for each type and merge errors. …

This blog post is for backend developers who want to learn the basics of technical SEO.

Image for post
Image for post

As a backend developer, my focus mostly goes to implementing new features, fixing bugs, optimizing SQL queries and so forth. What about SEO, though? Well, I’ve never learned that. It is a whole different skill set, a job on its own. Out of curiosity, and because I had the opportunity to build a super tiny one page static website, I took some time to learn about the basics of SEO. Bear in mind I’m not laying down advanced strategies, this is more like bullet points to make sure you’re not shooting yourself in the foot. …


Have you ever wondered how your Rails application boots? I mean, when you execute rails server, what happens?

Image for post
Image for post

To answer this question, we’re going to start by generating a new Rails 6 application (I’m currently running 6.0.0.rc1).

$ rails new iluvrails
$ cd iluvrails

./bin/rails

The starting point of the boot sequence is the rails executable. To simplify this blog, we’ll start our journey in ./bin/rails.

By the way, what is the rails gem? It’s a packaging for all the following:

$ gem dependency rails -v 6.0.0.rc1
Gem rails-6.0.0.rc1
actioncable (= 6.0.0.rc1)
actionmailbox (= 6.0.0.rc1)
actionmailer (= 6.0.0.rc1)
actionpack (= 6.0.0.rc1)
actiontext (= 6.0.0.rc1)
actionview (= 6.0.0.rc1)
activejob (= 6.0.0.rc1)
activemodel (= 6.0.0.rc1)
activerecord (= 6.0.0.rc1)
activestorage (= 6.0.0.rc1)
activesupport (= 6.0.0.rc1)
bundler (>= 1.3.0)
railties (= 6.0.0.rc1) …

Once upon a time, there was a Rails developer who wanted to become a Rails contributor. Deep down he even wanted to become a Rails core team member! So one day he decided to take a first step: he downloaded the source code of Ruby on Rails!

Image for post
Image for post

So I downloaded the source code of Rails:

$ git clone git@github.com:rails/rails.git
$ cd rails
$ ls
actioncable
actionmailbox
actionmailer
actionpack
actiontext
actionview
activejob
activemodel
activerecord
activestorage
activesupport
Brewfile
ci
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
Gemfile.lock
guides
MIT-LICENSE
package.json
rails.gemspec
RAILS_VERSION
railties
Rakefile
README.md
RELEASING_RAILS.md
tasks
tmp
tools
version.rb
yarn.lock

Damn.. so much stuff in here.. I know most of Rails components, but there are some novelties in here as well. Thankfully Rails has several guides to help me fill the gaps. …


Image for post
Image for post

Ruby on Rails, our beloved framework, provides a new standard for file upload. Please welcome ActiveStorage!

This article is a super quick, straight to the point guide to get started with ActiveStorage.

I’ve hosted a working example application on Github for you to try it out-of-the-box. It illustrates most of what this article presents. The link is at the end of the article.

Table of content:

  1. How to add ActiveStorage to your Ruby on Rails 5.2+ project
  2. How to choose where to store uploaded documents (on local disk, on Amazon S3, etc.)
  3. How to make a model have one attachment…

La productivité, c’est le saint Graal. Le problème, c’est que l’on a faux sur toute la ligne.

Image for post
Image for post

Petit historique de la productivité

La génération des baby-boomers (nés entre 1945 et 1960) a connu une approche industrielle de la productivité. Comme à l’usine, on leur a expliqué qu’être productif c’est faire plus en toujours moins de temps. Et comme des machines que l’on ferait tourner quelques heures de plus pour fabriquer un peu plus, les salariés devaient également faire des journées plus longues pour produire un peu plus.

Notre génération a vu la notion de productivité évoluer. Aujourd’hui, nous baignons dans un univers très inspiré des startups où il faut faire des to-do lists et du Pomodoro, autrement dit mettre en œuvre des techniques d’organisation. L’accent n’est plus mis sur la durée de travail (maintenant acquise) mais sur l’utilisation d’outils et techniques censés décupler notre rendement. …


Image for post
Image for post

StimulusJS is the new Javascript framework that’s been published this year (2018) by DHH, the creator of Ruby on Rails.

This is a practical guide to start using StimulusJS in a Ruby on Rails project (with a bonus at the end 😉). You’ll also find at the end of the article a link to a project that uses all that’s presented in this article for you to try it out of the box.

You might be tempted to compare it to other frameworks such as ReactJS, Angular or VueJS but you should not. StimulusJS doesn’t play in their field since it doesn’t intend to build user interface components, synchronize frontend and backend using AJAX or any of this stuff. …


Image for post
Image for post

Working on a new project, I finally had the opportunity to try Rails 5.2 new way of handling encrypted credentials.

So far we’ve been used to:

  • environment variables
  • dotenv files .env.#{Rails.env}
  • secrets
  • encrypted secrets

Rails now ships with a new standard for storing credentials, so let's have a look at it.

Files involved

The first thing to notice is that Rails 5.2 projects come with two new files:

  • config/credentials.yml.enc: encrypted yaml storage (your secret values are stored here)
  • config/master.key: encryption key

Basically, credentials.yml.enc is encrypted with the secret key that's stored in master.key.

Note that master.key is ignored in .gitignore and should not appear anywhere in your git repository. We'll talk later about how this encryption key should be stored and used on your production server(s). …

About

Younes SERRAJ

Ruby on Rails developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store