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’

Use vi / vim to remove ^M characters from files

Sometimes you’ll be handed a file from a Windows user and it will contain a bunch of “^M” characters at the end of every line.  Removing them is easy, just run the command:


To get the “^M” properly, don’t type “^” and “M”.  Instead, type “ctrl + v”, then “ctrl + m”.