Ruby on Rails or Phoenix ?

Thomas Galibert
3 min readDec 5, 2020

For my first article about Elixir (it’s the language) and Phoenix (the framework based on Elixir), I just want to share my first feelings when delivering my first Elixir project.

Illustration by Katerina Limpitsouni, https://undraw.co/

Since several months, I started to learn Elixir and Phoenix. Maybe the only positive point of the Covid-19, I had more free time.

Why Elixir ? Because of my Ruby background for more than 10 years now and my twitter timeline that suggested some posts about. And the interest in learning a functional programming language, something fresh in my day to day routine.

I began to read some books (Elixir in action, Phoenix in action, The little Elixir & OTP Guidebook, and Programming Elixir), because it’s my way to learn over screencasts. I like to take my time with some code and a cup of mint tea.

At this time, I had a project for a client, an oyster farmer : making an application to allow his customers to order online, then formatting and sending the orders to the ftp of his accounting platform. Nothing very difficult here, but it was a good case to try my new knowledge.

This case is indeed a good candidate because I want ideally stream the content of large csv files and update the database accordingly (product, category, prices, …). You can do it well with Rails obviously, but it’s not an easy task and that’s one of the thing a developer has to do someday.

I can’t (and it’s not the purpose of this article) share all the code here, but you can see below an example that updates the product’s catalog :

Here you can see why I love Elixir with the self explanatory quality of the import function. The pipe operator decomposes the work into 4 elementary tasks :

  • download the csv file of the products from the FTP (the excellent and bulletproof Erlang ftp library helps)
  • stream the file
  • decode the csv
  • apply the fill_produit/1 function (sorry, some french words here ;)

Basically, the rest of the module takes the products attributes and checks if the product already exists in the database : if yes updates, if not, we creates a new one (don’t worry if you not familiar with the syntax and some concepts specifics to the Ecto library, the database wrapper and query generator for Elixir, that I will cover in future posts).

I was impressed how I could achieve almost all the important pieces of the application in one day without any serious difficulties.

If I compare to the same tasks in my typical Rails projects, it’s demanding more time to stream (asynchronous processes the file in chunks) large csv files like this. I did not make benchmarks, but, guys, believe me, you don’t need it to feel the huge difference.

Finally, I could end up this project sooner than I predicted with a rock solid application.

I’m not saying Phoenix is better than Rails, just that having a new tool in my box is a good thing. Trying a different point of view (functional programming) makes me writing better ruby code as well. I’m happy to see the last ruby iteration (~> 2.7) adopting elixir’s pattern matching¹.

The two ecosystems are feeding each other. For example, we have now the same approach of building rich single page application without a javascript framework : Liveview for Phoenix and SitmulusReflex for Rails. The both use websockets to keep the DOM updated. But we cover this in a next post !

--

--