ruby on rails – bundle reinstall

After an OS upgrade I needed to reinstall all the gems for my Rails project (well, I probably didn’t need to do it, but it seemed the quickest way).

Since I used RVM and only had one Ruby on Rails project I was working on, doing so is easy, though not intuitive.

To uninstall all the gems:

$ rvm gemset empty

Then install them the regular way:

$ bundle install 

You might get an error that looks like this: “There was an error in your Gemfile, and Bundler cannot continue.”.  If that’s the case, run:

$ gem install bundler

Ruby on Rails unit test: cannot find table error

If your Ruby on Rails unit tests complain about not being able to find a table after you’ve added a new model, it just means you forgot to prepare the test database with the latest data.  The error looks like this:

ActiveRecord::StatementInvalid: Could not find table ‘yourtablenamehere’

To fix, just run:

rake db:test:prepare

and then re-run your test.

Error in rake db:migrate: rake aborted! uninitialized constant Rake::DSL

I’ve never had this issue before, but I recently started using rvm and for whatever reason got the following error when trying to do rake db:migrate

rake aborted!
uninitialized constant Rake::DSL
/usr/local/Cellar/ruby/1.9.2-p290/lib/ruby/1.9.1/rake.rb:2482:in `const_missing’

After googling around and trying a bunch of convoluted solutions (many which addressed a broken 0.9.0 build of rake), I found that adding the following line to my Rakefile fixed everything:

require ‘rake/dsl_definition’

Get Familiar with Ruby using EdgeCase’s Ruby Koans

EdgeCase has published a great walkthrough of the various Ruby language features. They call it Ruby Koans.  The Ruby Koans are a a collection of broken tests.  Each broken test is designed to teach you something about the Ruby language.  Many are fill-in-the-blank, eg:

  def test_every_object_has_different_id
    obj = Object.new
    another_obj = Object.new
    assert_equal __, obj.object_id != another_obj.object_id
  end

To pass this koan/test, you’d replace the “__” with true, because new instances of objects have new object_ids.

I wouldn’t recommend this for people new to programming, but if you’re like me and used Ruby a few years ago but have fallen out of practice, it’s a perfect re-introduction.  If you’re new to Ruby but have a solid background in other languages, this may be of use as well.