Rails is Magical? Maybe You’re Scared

Joel Blum
4 min readFeb 17, 2018

--

It was both funny and sad seeing a colleague of mine taking a deep dive into our Rails project, after having worked mainly in c++ for 9 years.

What method is being called here? I searched everywhere and can’t find it! What is this controller doing? I don’t see anything being returned

And so on and so on.

Googling “Rails magic” shows this sentiment is shared by many to the point Rails (and maybe Ruby too) are now notoriously “magical”.

There are 2 issues with this sentiment:

  1. It’s bullshit. I’ll compare Rails to other frameworks to show you why. In fact, I’m having a real hard time finding another MVC framework as well documented as Rails(be it the official docs or other tools like Stackoverflow)
  2. It shows that as programmers we really suck at taking the time to read the documentation. We either think our knowledge base transfers completely to a new ecosystem or we just get pressured by managers to produce code fast and now — so we don’t bother. Sometimes there are psychological reasons preventing us from progressing in new knowledge bases.

Rails isn’t magical, it’s opinionated and does a lot for you. That’s it.

Off the top of my head here are the things that confuse Rails newbies

  1. Implicitness: Controllers that render things without explicitly being programmed to do so. Counter caches being added with no idea why it’s happening, etc. No importing.
  2. “Magic Methods”: methods that can’t be found in the codebase. Usually ActiveRecord methods defined dynamically like find_by, enums etc.

“Magic Methods”

First, Django (which promotes ‘explicit is better than implicit’) uses them too.

Second, and most importantly, they are all covered in depth in the Rails documentation; there’s no reason for a developer not to know about them.

Third, the ability to so easily add dynamic methods on the fly, meta-program and create DSL’s — is one of the things that sets Ruby apart. It’s one of the strong points of Ruby and Rails, not something to get angry about.

I know it’s not consensus, but I love the fact I can

User.find_by_email(“greg.stephan@gmail.com”) 

Without having to define a method for each User field. Rails rocks sometimes.

Implicitness

Rails can be implicit to save you from typing the same thing again and again(DRY). These are just conventions that can easily be understood by reading the docs, here are some of the main ones:

  1. Controllers rendering views without being explicitly told to do so.
  2. All controller instance variables are made available to the view, you don’t need to set up a context like you would in Django or java. You can always use different solutions if you don’t like it

3. Everything is already “imported”. You don’t need to write import 20 times on each file like you would in java and python- everything (your models, gem classes etc) is “magically” imported into the namespace for you in Rails. Again, we can argue about the pros and cons of this (I actually really like it), but once you understand it — where’s the magic?

While not rocket science, modern frameworks are not easy. They give you very sharp knives to use, and if you don’t have the dedication to actually sit down and learn them — you will cut yourself. Don’t confuse ease of starting (creating a new project, running a local server etc) with the ability to write quality code.

Another point of complain I hear a lot (particularly from people new to Ruby)

the syntax is like nothing I’ve ever seen! It’s weird, what are those ‘do’ things everywhere?

Well, I’ve been recently deep diving into Spring MVC. Here’s a simple controller:

@Controller
public class HelloController {

@GetMapping("/hello")
public String handle(Model model) {
model.addAttribute("message", "Hello World!");
return "index";
}
}

What are those weird @ signs? Are they like python annotations? Why don’t they use regular OOP inheritance like they do in Django or Rails? What is this magic???

We are all out of our depth sometimes, and when we dive into a new technology we sometimes get anxious. Hey, I absolutely suck at Spring MVC at the moment. In fact, a fresh bootcamp grad might be more productive in Spring MVC than me. This is the point when we tend to stick to the languages and frameworks we know, and resist change. I know the feeling all too well.

So when I now sit down to learn Spring MVC I have 2 choices:

  1. Admit ignorance, start diving into the docs and try very simple things at first, slowly progressing and building confidence.
  2. Yell “MAGIC! Blasphemy! Java is terrible” and remain a Rails developer till I die.

I think we should choose option 1 if we want a long and interesting career in this business

This story is published in Noteworthy, where 10,000+ readers come every day to learn about the people & ideas shaping the products we love.

Follow our publication to see more product & design stories featured by the Journal team.

--

--