Adrien Siami

Software engineer, Ruby, Rails, Web and stuff.

Read this first

Diving in Rails - Exceptions handling

Ruby code, just like any other code, can be subject to failure.

Fortunately, when an error occurs inside your Rails application, your rails server does not crash and stop serving requests.

In this article, I’ll explain how rails catches exceptions that may happen in your code, and how it renders a nice error page, depending on the Rails environment, and how you can customise it.

I won’t be talking about rescue_from, and ActionController::Rescue, but about the rack part.

 Remember, it’s all Rack !

If you read my previous article about [Rails request handling](, you know that Rails is based on Rack, and uses middlewares for various things.

Rails also handles exceptions with middlewares.

This article will explain how exceptions are handled in a production environment. If you are interested in understanding how Rails displays errors

Continue reading →

Diving in Rails - The request handling


There is an aspect of Rails I both like and dislike, Rails is automagical.

It does a lot of complicated things for you and makes the job super easy, however, it can be kind of frustrating, this feeling of relying on some magical piece of code.

I wanted to have a better grasp on the core problems Rails try to resolve.

You probably know it, reading the source of software, frameworks, libraries, is one of the best way to learn how they work.

In this article, I’ll talk about the stuff I learned while reading Rails’s source code, it may be the first of an undefined number of similar articles.

Today I’ll speak about how Rails handles requests, its relation to rack and more globally what is happening when you submit a request to a HTTP server backed by a Rails application.


If you have ever worked with Ruby, you probably already know what Rack is.

If you know

Continue reading →

Use roles in your rails models for mass assignment

As you probably know, with ActiveRecord (more exactly ActiveModel), you can whitelist some fields of your models so they are safe to be mass-assigned from a form.

Sometimes, you want some fields to be alterable by normal users, and other fields to be alterable by admins.

What I did in this case for a long time was to use slice on the params hash or create a if / else statement if the user is admin then individually assign each field.

ActiveModel provide us a much better way to to this.

Let’s assume you have a User model :

class User < ActiveRecord::Base

  attr_accessible :bio

  def role
    self.admin ? :admin : :default

You can mass assign the bio field. There is also a role method that will tell us if the user is an admin or a default user.

Now imagine you want your admin users to be able to update other fields, for example, the login field.

Thanks to ActiveModel

Continue reading →

Add some regular ruby code in your rails migrations

For long, I’ve used rails migrations only as a way to update my database schema, no more.

Today, I had to do a more complex operation.

I have a Message model and a Conversation model; for historical reasons, the read / unread was stored in the Message model, but I actually don’t need to keep a track of what message was read or not, only conversations.

So I had to do a simple migration that would create a filed in the Conversation model and remove it from the Message model.

However, I’ll have to apply this migration to production, and this will cause some data loss, how can I mark as unread the unread conversations ?

A few months ago, I would have created a complex script for doing that.

Today, I tried something pretty intuitive :

class MoveUnreadToConversation < ActiveRecord::Migration
  def change
    conversations = {}
    Conversation.includes(:messages).each do

Continue reading →

My Ruby on Rails Stack

For deploying Ruby on Rails apps to production, I’ve previously used many different tools to get an idea of what will suit me the most.

Today, I think I’ve found what I wanted, so I’d like to share this with you, as picking around on the net config files and ideas was very useful to me.

Here’s the short version : I use Nginx as the frontal web server, it will act as a reverse proxy, sending all the requests from the browser directly to a unix socket on which several unicorn workers are listening. I use capistrano to automate the deployment, based on a github (or any git) repository.

I usually create 3 instances of the same website :

production (The master branch on the git repository)
pre-production (The preprod branch on the git repository)
development (The develop branch on the git repository)

In fact, I have 3 config files for nginx, 3 checked-out repositories (one for each

Continue reading →

Why I hate JQuery Mobile

If you follow me on twitter, you probably have seen me whining about JQuery mobile.

Indeed, I’ve worked a lot with JQuery Mobile recently, and this has been a pain.

So I’ll try to sum up here what I don’t like about JQuery, maybe I’m wrong, maybe I’m right, just tell me in the comments what you think about this.

 Bugs, bugs everywhere

If you have a conversation of more than five minutes with a developer that worked with JQuery Mobile, you’ll probably hear that JQM is full of bugs.

Well, after several months, I’ve been through a ton of them.

Some bugs are well-known and stackoverflow is full of useful workarounds dirty hacks.

This adds a considerable amount of time to the development time, and gives me the impression of being innefective, hate that feeling.


Whereas some parts of JQM are well documented, some are simply … not.

For example, list icons.
This page only

Continue reading →