I’m going to take a new approach with this article by first showing how quickly a Ruby On Rails application can be created.  At the conclusion of this article, I’ll go back and fully explain what we’ve accomplished.

To get started, open a PASE shell (i.e. CALL QP2TERM or SSH into your IBM i, as is my preference) and enter the following command:

Now change directory (cd) into the new application that was created and run the bundle install –local command:

We ran two commands and now have a fully fleshed out application that includes a web server and is ready to be started with the below rails server command.

Open up your browser and type your IBM i IP address followed by :8001, as shown below:

seat1

Next let’s create a customer table and generate a full CRUD (Create Read Update Delete) application:

Run the below command to have the table created in the database:

Now bring up your browser to the same page as before and add /customers to the end, as shown below.  Click on “New Customer”.

seat2 Fill out the form and select “Create Customer”.

seat3 Now go back to the index page (i.e. /customers).

seat4NOTE: SQLite was used as the database in this brief tutorial instead of DB2 for i.  Included with PowerRuby is the RAILSNEW 5250 command that accomplishes the initial creation of a Rails app using DB2 for i instead of SQLite.

In brief review we effectively ran the below five commands and now have a completely working, database driven, application.

Wow!  That’s neat and cool but what actually happened?

First, the rails new command is used to create the infrastructure of an application.  This is a huge time-saver that would normally take considerably longer.  One other big advantage is the infrastructure for Rails apps all look the same because they are started with the rails new command.  This makes it easier for new developers to quickly dive-in and comprehend the application.   Creating a new Rails application is so fast that I often times do it for prototyping so I can quickly test a concept or new Gem without effecting my existing application.  Included in this initial setup are configurations for each environment, preconfigured with conventional configurations.  For example, in the config/environments/development.rb file we saw in the earlier log has the config.cach_classes setting set to false.  This makes development go considerably faster because you don’t have to stop and restart your app to get the latest version of code.  This is in contrast to config/environments/production.rb which has config.cach_classes setting set to true – which is a good thing because we shouldn’t be changing code in production.

Next we have the bundle install –local command, part of the Bundler tool, which is used to make sure all necessary Gems specified in the Gemfile file are “bundled” on this machine and available to the application.  The –local option is specified because we don’t want the most recent versions of all Gems to be downloaded from RubyGems.org.  Why?  Because some of the Gems require compilation and that is one of the advantages of the PowerRuby install.  When bundle install is run it will create a Gemfile.lock file that details the exact versions of Gems that were proven to work in development.  This gives assurances that the production environment will have the same scenario as development.  Locking down Gem versions might initially seem like an odd need, but it is necessitated by how frequent Gems are updated – an advantage of an extremely active and progressive community.

At this point we start the web server with command rails server -p8001 and see the aforementioned “Welcome Aboard!” screen.  This is possible because Rails includes its very own web server named “WEBrick” – yet another convention over configuration.  The Rails folks realized that if you are aiming to develop web apps quickly then you most likely don’t want to be bothered to setup an application server.  Even though Rails includes the WEBrick web server you’ll find many instead use the Thin web server because it is more feature rich.  Since you always want your development environment as close to production as possible it is wise to use Thin while developing.  Switching to Thin is very easy.  Simply add the below line to your Gemfile file and run bundle install –local again.

Now when you run rails server -p8001 it automatically recognizes Thin is associated with the application and uses that instead.  Cool – (and efficient)!

Having the infrastructure of an application generated for you in seconds is a huge plus, but is only the beginning.

Next we see the use of the rails generate scaffold command which is used to create not only a new CUSTOMERS table but also an entire CRUD (Create Read Update Delete) application over that table.  The parameters for the scaffold command declare the columns for the table and inherently what HTML fields are declared in the UI layer.  Below is a sampling of the output from the scaffold command.  First notice how it made use of the existing infrastructure initially created with the rails new command – i.e. directories app/models, app/controllers, and app/views.  Not only did it generate everything necessary for the application to run but also infrastructure for unit testing – probably the most oft forgotten aspect of software development. [I raise my hand as a guilty party of not doing as much unit testing as I should].

One other thing generated with the scaffold command is what’s called a database migration –  “a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use an easy Ruby DSL to describe changes to your tables.”, according to the Rails Guides site.

Below we see the db/migrate/20150202155257_create_customers.rb file that was generated with the scaffold command.  Even though this might not look like Ruby syntax (i.e. create_table…) it actually is 100% Ruby.  This is what’s called a Ruby DSL, or Domain Specific Language.  This is a feature of Ruby it took me awhile to appreciate.  I now highly value it because code becomes more concise and fit to the task at hand – in this case the definition of a table.

The above database migration file leads us to our next command, namely rake db:migrate.  The rake command is a general purpose utility for Ruby that is similar to the make command.  Many RubyGems include a Rakefile file that describes various rake tasks that can be invoked by the developer.  Incidentally Rails is a RubyGem and when we ran the rails new command the file Rakefile was created.  Go here to learn more about how Rakefile relates to Rails.

The rake db:migrate command should be run whenever you deploy to production, whenever you pull other programmer’s changes into your development environment, or when you make database changes yourself – as is the case in this scenario.  What does it do?  When you run rake db:migrate it will query the schema_migrations database table (auto-created for you the first time you run a database migration) to determine if the DB2 for i database is up to date with what is defined in the migration files within your Rails application (i.e. db/migrate directory).  If it finds the DB2 for i schema is out of date it will then run the necessary migration files.  As previously mentioned, migration files are Ruby code.  This means you can add more code to a migration if necessary.  For example, if you originally created a CUSTOMER table with an address but needed to move that to an ADDRESSES table you could use a database migration to first create the ADDRESSES table, migrate the address column data to ADDRESSES, and then remove the address columns from CUSTOMERS.

I hope this gives you a good idea of the Rails advantage!

It’s important to note we’ve only skimmed the surface.  For example, while generating an entire C.R.U.D. application is good for some scenarios, there are a number of other generators available that can be used in more granular fashion – like generate a model that doesn’t need any portion of a CRUD app.  You’d use the rails generate model command in that case.

On final note, we have a free tutorial you can go through that shows how to add many more features to this application.  It will grow your knowledge of Rails and bring further realization of the big benefits.

Contact the Litmis Team to gain access to it!