Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 1

It's the ultimate JVM Web Framework Smackdown! Three will enter the cage. Only one will exit the victor. Will it be JRuby on Rails? Groovy on Grails? Or Helma?

Before the advent of Ruby on Rails, many Web developers worked with Perl or vanilla PHP. Perl’s legacy dated back to the earliest dynamic Web sites. In heady days, the “Perl Mongers” were active proponents and evangelists, and the community nurtured and maintained a vast repository of contributed code. PHP was simple to learn, a snap to deploy and host on virtually any platform, and thus quickly earned a devout following. Indeed, both languages grew popular because it was easy to start an application. However, as Perl and PHP applications grew, developers inevitably became tangled in a mess of code.

Other developers eschewed scripting languages and opted for more heavyweight Web frameworks, especially those written in Java. Applications written in Java were bullet-proof and backed by a rich set of libraries, but were far from perfect, too. Java frameworks exacted a high price, miring developers in the pits of XML hell. While the Java frameworks scaled well, the initial inertia left many Java web developers jealous of the rapid, early progress Perl and PHP developers enjoyed.

Ruby on Rails, to a large degree, delivered the best of both worlds. Rails featured a well-thought out organization that guided developers toward a clean MVC design, and through a philosophy of Convention over configuration, developing new applications was only marginally more difficult than with straight PHP. Web developers, it seemed, could finally have it both ways.

But the story does not end with the advent of Rails. More and more sites began to use Rails, but as several endeavors became successful, developers also ran into new challenges. Twitter gained notoriety when the company moved part of its architecture to Scala. And, despite the benefits of Rails, many developers have begun to miss the wealth of libraries and tools commonly found with Java.

Fortunately, several Java frameworks now combine ease and robustness. Relying on JVM scripting languages and the Rails philosophy of convention over configuration, these new frameworks provide the flexibility of Rails and the enterprise-worthy support of Java. This article compares three of the most promising modern Java frameworks: JRuby on Rails (JRoR); Grails, powered by Groovy; and Helma, with Rhino JavaScript.

The Sample Application: PenguinMusic

To test JRoR, Grails, and Helma, let’s build a music site from scratch with each framework. The sample site, PenguinMusic (shown), allows users to browse songs ordered by popularity, view details about an artist and album, and leave comments if logged in to the application. The application also handles user registration.

While this sample application does not take advantage of all the bells and whistles each framework offers, it does focus on a core subset of features that virtually every website must provide, including database interaction, user authentication, and form handling. The sample code also looks at automated testing and site administration to some extent. However, no attempt is made to build facilities for Ajax or address performance. Not every site needs Ajax support, and all these frameworks have at least adequate performance for the bulk of Web applications. The focus here is the ease of launching a new application.

Each of the three frameworks ships with an embedded databases, but odds are MySQL is your choice for a production database, so that’s what’s demonstrated here. However, since all the candidate frameworks rely on JDBC, you can interact with just about any major database engine.

If you would lke to follow along, you can download the source code for each implementation. Additionally, there are some helpful scripts to load test data for each of the database setups.

JRuby on Rails: The Classic, Now Playing at Your Local JVM

If you want the benefit of a Ruby on Rails-like framework for the JVM, one obvious option is, well, Ruby on Rails. The JRuby team has been focused on Rails from the beginning, and with JRuby 1.0, the team finally achieved full Rails compatibility. Performance was terrible initially, but the developers have done a great deal of work since and more recent versions are quite fast.

The source code for the JRuby on Rails implementation of PenginMusic can be downloaded here.

Getting Started with Rails, the JRuby Way

Installing JRuby on Rails is only a little more complicated than installing traditional Rails. You must download Gems and install it with JRuby and install the requisite libraries. The command to use is:

 $ jruby -S gem install mongrel activerecord-jdbcmysql-adapter rails

Running jruby -S rails jrorPM -d mysql creates a new application named jrorPM based on MySQL. After the initial code generation, your first task is to edit config/database.yml to match your database configuration. In particular, you must specify jdbcmysql as the adapter.

  adapter: jdbcmysql
  encoding: utf8
  reconnect: false
  database: jrorPM_development
  pool: 5
  username: root
  socket: /tmp/mysql.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  adapter: jdbcmysql
  encoding: utf8
  reconnect: false
  database: jrorPM_test
  pool: 5
  username: root
  socket: /tmp/mysql.sock

  adapter: jdbcmysql
  encoding: utf8
  reconnect: false
  database: jrorPM_production
  pool: 5
  username: root
  socket: /tmp/mysql.sock

Once the database has been configured, jruby -S rake db:create:all initializes the database, and jruby script/server starts the application.

Rails Models and Migrations

One of the hallmarks of Rails implementations is that the structure of the data model lives solely in the database. This avoids any repetition of logic, a core tenet of Rails’s Don’t Repeat Yourself (DRY) philosophy. However, most of the validation logic lives in the Ruby models. As a result, one must switch between the database table and the Ruby class to get a complete picture of the model. However, this is a relatively minor hindrance, as the strong connection between the database and the model class is one of Rails’s greatest strengths.

Once upon a time, Rails developers used SQL to create models, but the practice has gradually fallen out of favor. Today, developers are encouraged to use migrations instead. Migrations are Ruby scripts that create or update a database schema. A migration is also database-independent and is fairly straightforward to use. Migrations also offer an important advantage: versioning. Historically, tying a database version to a code version has been problematic. Migrations offer a solution.

However, migrations can also be brittle. If there is a problem in any of the migration scripts, the database can become stuck in an unusable state, forcing you to resort to SQL to fix the problem anyway. Migrations also are noticeably lacking in features. For instance, foreign key constraints are not available without a plugin.

SQL does provide fine-grained control and is an option with Rails, although you may be heckled by some Rails fanatics if you choose to use it. Here, let’s stick with migrations since they are the more conventional approach.

The Rails generate script creates new models and other components from the command line. Specify model as the first argument if you just want to create the required model class. More often than not, though, you’ll want to specify scaffold instead to generate a model and complementary administrative pages to create, edit, and delete records. The second argument to generate is your model name, and the remaining arguments are name:type pairs, one pair per field. (You can actually omit all of the fields and manually edit the migration script yourself if you prefer.)

To generate the code for the Song model and scaffold, the command is:

$ jruby script/generate scaffold song name:string album_id:integer \
  track_num:integer playlist_url:string duration_in_seconds:string \

The command creates a file named db/migrate/*_create_songs.rb (the * is a serialized value that orders the migrations in a sequence) with the following contents:

class CreateSongs < ActiveRecord::Migration
  def self.up
    create_table :songs do |t|
      t.string :name
      t.integer :album_id
      t.integer :track_num
      t.string :playlist_url
      t.string :duration_in_seconds
      t.integer :num_downloads


  def self.down
    drop_table :songs

Although you can use this migration as-is, let’s modify the code to improve the database representation. Let’s make album_id and name required fields and let’s assign a default value for num_downloads. This requires changes to three lines:

      t.string :name, :null=>false
      t.integer :album_id, :null=>false
      t.integer :num_downloads, :default=>0

The process is repeated for the other models, followed by a database update:

$ jruby script/generate scaffold album artist_id:integer genre_id:integer \
  title:string public_id:string url:string album_cover_url:string \
  album_cover_name:string price:float blurb:text
$ jruby script/generate scaffold artist name:string url:string
$ jruby script/generate scaffold genre name:string code:string
$ jruby script/generate scaffold user username:string \
  password:string email:string admin:boolean
$ jruby script/generate scaffold comment user_id:integer album_id:integer \
  title:string body:text post_date:datetime
$ jruby -S rake db:migrate

As a final step, the models must be tied together. This is done through the has_one, has_many, belongs_to, and the concisely named has_and_belongs_to_many functions.
As an example, here is the model class for Album:

class Album < ActiveRecord::Base
  belongs_to :artist
  belongs_to :genre
  has_many :songs
  has_many :comments

Comments on "Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 1"

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses. But he’s
tryiong none the less. I’ve been using WordPress on various websites for about a year and
am anxious about switching to another platform. I have heard excellent things about blogengine.net.
Is there a way I can transfer all my wordpress content into it?
Any help would be greatly appreciated!

It is necessary to maintain control of one’s thoughts while
performing almost any trading.

Also visit my blog forex brokers in cyprus (https://www.eventbrite.com/e/best-forex-brokers-tickets-18350646277)

Let us start with what the currency exchange rate is.

It is the pace of which one currency could be exchanged for another

Review my website http://www.panorama.it/magazine/tasse-risparmi-conti-correnti/

A lot more essential, you’ve to master what can cause rates to go along or up and regarding the foreign exchange marketplace.

Feel free to surf to my weblog; http://24h.net.au/lifestyle/meet-the-real-life-popeye-and-his-big-hands/

forex trading platforms [kcraftmc.com] is foreign exchange trading and it’s also the
biggest lively most liquid marketplace on the planet.

Admiring the dedication you put into your blog and detailed information you provide.
It’s awesome to come across a blog every once in a while
that isn’t the same unwanted rehashed information. Great read!
I’ve bookmarked your site and I’m including
your RSS feeds to my Google account.

They can be generally grouped into two sort of strategies are profit maximizing strategies decreasing strategies.

Here is my web-site: forex trading basics (data.adliberum.com)

InstaForex providers are of the good attention for over 2
000 000 forex brokers in pakistan (http://otobe4.tea-nifty.com/) traders all around the planet nowadays.

I’m really enjoying the theme/design of your weblog. Do you ever run into any
internet browser compatibility problems? A handful of my blog readers have complained
about my site not operating correctly in Explorer but looks great in Chrome.
Do you have any ideas to help fix this issue?

Trading – so that you can consider tiny gains, new dealers may place extremely limited requests.

Feel free to surf to my web-site forex brokers in cyprus

I am a Chartered Accountant, stock and forex market broker, business owner and home investor.

Feel free to visit my webpage :: http://pipeclass.com/index.php/en/guestbook-libro-visitas

I’ve chosen to wander along the less- sailed road and ventured
to the sphere of trading the a lot longer-term graphs.

Here is my web site: forex brokers in nigeria (Loretta)

Because period of time the persons at FxKeys and also you have coached me how exactly to have a life trading Forex.

Visit my blog :: http://www.linux-mag.com/id/7190/

Instead of seeking for weeks while risking your hard
earned cash, to crack the forex brokers in kenya (http://prezent-tour.ru/index.php?option=com_k2&view=item&id=101:navostok&con=996/rss/news/index.xml) rule, take a moment and feel.

The key elements forex brokers in india-stock trading is usually to not be
unable to follow rigid trading principles and
having self discipline.

For key providers, how big the party and the waiting time are inversely

my blog … health insurance australia

Insurance providers offering class health insurance quotes-insurance are forbidden legally from denying coverage based on ones own recent health or

Great, thanks for sharing this article.Really thank you! Keep writing.

One of our guests recently advised the following website.

Awsome blog! I am loving it!! Will be back later to read some more. I am taking your feeds also.

Say, you got a nice blog article.Much thanks again. Fantastic.

Thank you for great information I was on the lookout for this info for my mission.

This is one awesome post.Really looking forward to read more. Much obliged.

Check beneath, are some totally unrelated websites to ours, having said that, they may be most trustworthy sources that we use.

This is one awesome blog article. Fantastic.

Although websites we backlink to below are considerably not associated to ours, we really feel they may be truly really worth a go as a result of, so have a look.

Just beneath, are various entirely not connected sites to ours, on the other hand, they’re surely worth going over.

Below you will come across the link to some web sites that we consider you need to visit.

Fantastic blog article.Really thank you! Fantastic.

I am so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best doc.

Looking forward to reading more. Great blog.Really thank you! Really Great.

Really informative blog article.Thanks Again. Will read on…

One of our guests just lately encouraged the following website.

wow, awesome article. Really Cool.

“obviously like your web site but you need to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I in finding it very bothersome to tell the truth on the other hand I will definitely come back again.”

One of our visitors lately suggested the following website.

Sites of interest we’ve a link to.

The data mentioned within the article are a few of the ideal readily available.

Leave a Reply