I’m a longtime ASP.NET developer, and indeed that’s how I’ve made my professional living for many years now, but I have to say there is a lot to love about Ruby on Rails.
There’s a simplicity and common sense approach that I really like.
I think other ASP.NET developers would enjoy certain aspects of Rails as well, so I put together a list of my 5 favorite things (in no particular order):
If you’re like me, you really don’t like writing database change scripts in SQL. Wouldn’t it be nice if you could write the scripts in your favorite programming language rather than resorting to T-SQL? Well, with rails migrations you can!
You write your schema changes as a ruby class (or even generate them) and run it at the database to modify your schema. For example, to create a posts table, on the command line just type:
$ script/generate model Post name:string content:text
This will automatically generate the required class for you which looks like this:
class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.string :name t.string :title t.text :content t.timestamps end end def self.down drop_table :posts end end
Then you just run “rake db:migrate” at the command line and your table is created. The framework even keeps track of which migrations have been run and versions them all so you can roll back to previous DB versions at any time.
2) Active Record
I love how active record does not force you to model out all your fields as properties. You simply build your table with migrations and then create a class of the same name that derives from ActiveRecord and Rails figures out the rest. No config files to set up or angle brackets to mess with.
It adds all the properties based on the table columns and a bunch of helper methods for finding records and working with the dataset, but your code is as simple as this:
class Post < ActiveRecord::Base end
With Rails a lot of decisions are made for you and that’s a good thing! Instead of endlessly debating which of the 10 ORMs to use for your data layer, you just use Active Record because that’s what’s built-in out of the box and works for 90% of the cases.
Rails has a specific way of doing things, and if you buy into it, you can let the framework take care of a lot of the little details for you.
There’s something wonderful about not having to fire up a huge IDE to do development. Just launch your favorite text editor and start writing code. My personal preference for writing Rails code on windows is the E-TextEditor (which is modeled after TextMate), but I could just as easily use notepad++.
This simply would not be possible with ASP.NET as there are just too many frameworks and libraries to deal with as well as just a lot of code to write that you almost need the IDE’s help to sort them all out. Not to mention, you need the IDE to compile your code (or the cmd line with hundreds of switches) which is not necessary in the the interpreted world of Ruby. The code and run cycle is almost instantaneous.
5) Less Code
After porting an ASP.NET MVC app to rails I was amazed with how little code it took in rails. The rails version was only about 25% of the code that was required for the ASP.NET MVC version.So, how is that possible?
Much of this is due to Active Record and the fact that I didn’t have to write any DB connector code, but also Ruby’s terse syntax and lack of strong typing just means that the amount of code you write is much less.
For example, a typical controller method in Rails looks like this:
def show @post = Post.find(params[:post_id]) end
And then @post is automatically visible in the corresponding view. Nice and elegant – the way coding should be.