6 Reasons I Love Rails

I have been a Java developer since 1999 and have personally been involved in over 30 projects that put Java code into production. I have sold over 100 such projects. I have worked with more frameworks and app servers and IDEs than I care to admit. It may come as a surprise then that after only 6 Ruby on Rails projects, I have to admit that I love developing in Rails and it has become my goto environment of choice for many projects.

1432925926897

Here is a list of 6 of my favorite things about Rails. Some of these points are more factors of Ruby than of Rails or of the Rails ecosystem than Rails itself, but honestly the distinction doesn’t matter as much as it might seem. The point is that it is easy to build pretty much anything in Rails.

1 – Migrations

One of the hardest things to manage in any greenfield project is the evolution of the data model. And Rails nails this with migrations. They are automatically timestamped, support rollback, and document the decisions that were made over time. Rails manages internally which migration have been run, and the developer just has to run rake db:migrate after a pull from git to run the DDL to create the latest tables, indexes, constraints, etc. Did you not want the last 3 migrations after all? Simply run 3 rake db:rollbacks. The syntax for creating migrations is simple, but you really rarely need to know it. Just run the command rails g migration table field:type and it will create the migration with the correct name/timestamp and all related code. Or if you want to create the model and migration together, change the command to rails g model table field:type.

1432925974763

2 – Tests

I will not get into the religious wars of Test Unit vs Cucumber. Both are awesome and both lend themselves to much better testing than is typically seen. Test fixtures utilize YAML to make it a breeze to set up and retrieve test harness data. The following fixture:

1432926028246

means that to access this fixture in any test case code, all we do is acct = accounts :test_account. Assertions are dead simple and there is all sorts of sugar provided to simplify creating test objects. Once tests are written, a simple rake test will run all of the tests and provide a simple summary.

3 - Dates

Working with temporal data in Java or really most programming languages……well, it builds character. Not so in Rails. Need a date 3 days ago? Forget about creating Calendar objects and doing complicated math on milliseconds past the epoch. 3.days.ago. Want 3 weeks in the future? 3.weeks.from_now. Programming with java.util. Date suddenly feels barbaric.

4 – ActiveRecord

I love the work that has been done in the Java world on ORM tools, especially Hibernate/JPA. But Rails offers an approach that is simpler, more flexible, and way more powerful. Sure, much of the power owes to the elegant sugar of the Ruby programming language, most notably lambdas. And sure, Java 8 introduces lambdas and surely Hibernate will borrow a page here. But the approach of using active data objects means that the developer writes less boiler plate code and less application code once the boiler plate has been set. The following snippet shows a query from the Reward Summit code base that chains together multiple scopes to execute some fairly complex SQL.

sims.by_sim_key(sim_key)
.not_baseline
.limit(num_cards)
.order('would_get desc')

5 - Gemfile

It may break your heart to not be able to sift through pom.xml files any longer. The following shows how bloated and verbose these tend to be.

1432926063399

Yes, there IS a better way. Rails calls it the Gemfile. The following sample is clearly less verbose, more expressive, and more self-documenting.

1432926088950

6 – Collections

Adding an item to an array or a list (beautiful that we don’t really even need to know right?) is dead simple. credit_cards << my_credit_card will get the job done. Need to loop through each credit card and do something? credit_cards.each { |cc| cc.destroy }. Want to create an array of credit card names? credit_cards.map{|cc| cc.name }. Want to add up the balance on all credit cards? credit_cards.inject(0) {|res, cc| res + cc.balance}. Sure, you CAN do this all in Java, but it would be quite a few more lines of code.

Rails is not yet acceptable for every development shop in the world, but because of these and many other features, any shop that wishes to more quickly develop testable, extensible applications should seriously consider adding it to the list of approved tools.

Related Posts