The following blog post contains material either currently found or soon to be incorporated into my new book, "Easy Active Record for Rails Developers". Among many other topics, you'll learn about model generation, migrations, validations, associations, scopes, joins, includes, forms integration, nested forms, and model testing with RSpec and FactoryGirl. The book is now available, head over to this website's home page to learn more.


The Rails console offers a no-frills interface for experimenting with and peering into all aspects of your application, Active Record objects included. The lack of frills can admittedly become a tad tedious though when peering into larger objects, as demonstrated by this example:

>> g = Game.find(124)
  Game Load (0.1ms)  SELECT  `games`.* FROM `games`
    WHERE `games`.`id` = 124 LIMIT 1
=> #<Game id: 124, name: "Zaxxon", description: "A vintage arcade game.",
   created_at: "2014-07-21 15:42:21", updated_at: "2014-07-21 15:42:21",
   release_date: 1982, manufacturer_id: 2, slug: "zaxxon">

Fortunately, there’s a fantastic gem called Awesome Print that allows you to have your console cake and eat it too, automatically applying formatting and syntax highlighting to your objects.

Here’s the same example, but this time formatted using Awesome Print:

>> g = Game.find(124)
  Game Load (0.1ms)  SELECT  `games`.* FROM `games`
    WHERE `games`.`id` = 124 LIMIT 1
#<Game:0x007f951a7af8f8> {
                 :id => 124,
               :name => "Zaxxon",
        :description => "A vintage arcade game.",
         :created_at => Mon, 21 Jul 2014 15:42:21 UTC +00:00,
         :updated_at => Mon, 21 Jul 2014 15:42:21 UTC +00:00,
       :release_date => 1982,
    :manufacturer_id => 2,
               :slug => "zaxxon"
}

Awesome Print is also useful for reviewing a collection of objects:

>> Game.limit(2).to_a
  Game Load (0.3ms)  SELECT  `games`.* FROM `games`  LIMIT 2
[
    [0] #<Game:0x007fab4a955ba8> {
                     :id => 1,
                   :name => "1942",
            :description => "A vintage arcade game.",
             :created_at => Mon, 21 Jul 2014 15:42:20 UTC +00:00,
             :updated_at => Mon, 21 Jul 2014 15:42:20 UTC +00:00,
           :release_date => 1984,
        :manufacturer_id => 1,
                   :slug => "1942"
    },
    [1] #<Game:0x007fab4a955770> {
                     :id => 2,
                   :name => "After Burner II",
            :description => "A vintage arcade game.",
             :created_at => Mon, 21 Jul 2014 15:42:20 UTC +00:00,
             :updated_at => Mon, 21 Jul 2014 15:42:20 UTC +00:00,
           :release_date => 1987,
        :manufacturer_id => 2,
                   :slug => "after-burner-ii"
    }
]

You can also easily review a model’s attributes simply by referencing the model name, like so:

>> Game
class Game < ActiveRecord::Base {
                 :id => :integer,
               :name => :string,
        :description => :string,
         :created_at => :datetime,
         :updated_at => :datetime,
       :release_date => :integer,
    :manufacturer_id => :integer,
               :slug => :string
}

Plenty of other options exist, including integration with the Rails logger. See the project README for a complete summary of what’s available.

To install Awesome Print and integrate it into your irb and Rails consoles, begin by adding the following line to your project Gemfile:

gem 'awesome_print', group: :development

After updating your project bundle, open up your IRB preferences file .irbrc, and add the following two lines:

require "awesome_print"
AwesomePrint.irb!

Reboot your project’s Rails console, and Awesome Print’s awesome features will be at your disposal!


Like what you read? There’s plenty more where this came from in my new book, “Easy Active Record for Rails Developers”!

Comments