Is Rails magic?
June 4th, 2007
I've been known to do a few card tricks in my time. As grown-ups we know that there is a logical answer, but we can still be impressed by something that we don't yet understand. This is what 'magic' is. Something impressive that we don't understand.
When you learn a card trick, you still get enjoyment from seeing it performed, but the 'magic' is no longer there. You have an appreciation of the effort that goes into to looking effortless.
It's the same programming. If you are new to Rails, this can seem magical:
Person.find_by_first_name("Steve")
Where does that method come from? It's not in my models. Pretty magical. But as with card tricks, you know there is a logically explanation.
Later on, you dig into the Rails source code and find this:
def method_missing(method_id, *arguments)
if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
Ah right. So Ruby has something called method_missing. That gets called if an invoked method doesn't exist. And look - there is a regular expression that splits out the column from the method call: 'first_name'.
Yeah, there's a lot more after these two lines in ActiveRecord::Base, but the 'magic' is starts to break down when we see this.
To a greater or lesser degree, all programming languages are designed to protect us from things we don't need to deal with daily or aren't directly relevant to the problem we're solving. What makes something 'magic' is lack of knowledge and it's that that can be a problem - not the 'magic' itself. Once you understand what's underneath, it's just a convenient trick to make something easier.
I say keep the magic, but strive to understand it:
- How does ActiveRecord figure out the correct table for my model?
- How does Rails know which template to open for my actions?
or even Ruby:
- How does 1 + 2 work?
- What makes Ruby not care about data types?
3 Responses to “Is Rails magic?”
Sorry, comments are closed for this article.

June 5th, 2007 at 10:02 AM
Yep, find_by_* is a nice example of rails magic. Magic that helps to clean your code is excellent, but sometimes it's very hard to understand why something "just happens". And you might not even know some magical stuff like automatic database timestamps without going through somewhat extensive api documentation or reading numerous rails blogs.
June 5th, 2007 at 10:42 AM
True. But things like automatic timestamps don't tend to happen unless you specify the datetime columns *_at.
This is also part of the point I'm making. Magic is defined by a lack of understanding. That's not always a bad thing. I don't know how all the bits of my car work but that doesn't stop me driving.
If I were a mechanic though, I would have more power if I had more knowledge. I might drive different or even be able to improve parts of my car to make it run better.
Breaking down the magic is part of the learning process.
June 5th, 2007 at 06:24 PM
You're right. Understanding the magic is part of the learning process, but with Rails it can be steep.
I don't think that cars are analogous. Car can be driven by anyone and it is designed so. On the other hand Rails is a programming framework by programmers for programmers. The magic stuff is only for those who code, not for the end users.
This is not to say that the magic is a bad thing. It's just that in my experience that could have been documented better. Actually most fun and gratifying parts of learning Ruby and Rails have been those moments when I have understood where the magic comes from, how is it done and why is it so elegant.