Rails is crazy powerful. There is so much that can be done with so little effort due to the incredible library of methods and commands built in to this framework. That “magic” frightens and confuses me. I don’t like typing in a large command having it do it’s magic with out knowing at least part of what’s happening. Rails is not magic. It is fantastic. Here’s why:
Wouldn’t it be great if magicians immediately explained how they did the trick? That’s what Rails gives us! What does “form_for” actually do when you call it? What does Rails actually do when I type in “rails generate model test”? It’s all there! It’s beautiful and understandable!
HOLY WOW! That’s some complex code, but it is readable. Take a look at this code:
<% module_namespacing do -%> class <%= class_name %> < <%= parent_class_name.classify %> <% attributes.select(&:reference?).each do |attribute| -%> belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %> <% end -%> <% attributes.select(&:token?).each do |attribute| -%> has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %> <% end -%> <% if attributes.any?(&:password_digest?) -%> has_secure_password <% end -%> end <% end -%>
When you call “rails generate model test name:string story:text age:integer” it uses that template to make this model:
class Test < ActiveRecord::Base end
To generate the migration, it uses this template:
class <%= migration_class_name %> < ActiveRecord::Migration def change create_table :<%= table_name %> do |t| <% attributes.each do |attribute| -%> <% if attribute.password_digest? -%> t.string :password_digest<%= attribute.inject_options %> <% elsif attribute.token? -%> t.string :<%= attribute.name %><%= attribute.inject_options %> <% else -%> t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> <% end -%> <% end -%> <% if options[:timestamps] %> t.timestamps <% end -%> end <% attributes.select(&:token?).each do |attribute| -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true <% end -%> <% attributes_with_index.each do |attribute| -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <% end -%> end end
And creates this migration:
class CreateTests < ActiveRecord::Migration def change create_table :tests do |t| t.string :name t.text :story t.integer :age t.timestamps null: false end end end
Isn’t that neat! The magic is slowly fading. Being a huge nerd, my favorite show is Star Trek: the Next Generation. In season 3 episode 4, Picard saves the day by explaining his advanced technology to someone from a bronze era civilization. I believe it’s particularly relevant here:
Now suppose one of your cave-dwelling
ancestors could see you
as you are today. What would she
I don’t know.
Put yourself in her place. You
have a power she lacks. You can
kill a hornbuck from a great
Only because I have a bow.
She has never seen a bow — it
doesn’t yet exist in her world.
To you, it’s a simple tool. To
her, it’s magic.
I suppose she might think so.
And how would she react to you?
I think… she would fear me.