Creating models in Rails: Capitals Can Kill!
My continuing foray into the world of Ruby on Rails have turned up a new oddity when creating models. I want to have a table that holds details about web URLs describing specific events. I thought a good name for this model would be EventURLs, so I generated my model with the following:
generate model EventURL title:string url:string accessible:integer last_check_date:date last_access_date:date archive_url:string
Everything was lovely – the model, migration and test files were all created. However, when I tried to run the migration, I got the error:
uninitialized constant CreateEventUrls
The conventional wisdom on the net is that this error is caused when the name of the migration file doesn’t match the name of the class inside it, but in this case it all looked fine:
$ cat 20130511195617_create_event_urls.rb
class CreateEventURLs < ActiveRecord::Migration def self.up create_table :event_urls do |t| t.string :title t.string :url t.integer :accessible, :limit => 1
However, it isn’t – the filename 20130511195617_create_event_urls.rb means that Rails expects the class name to be CreateEventUrls, and not CreateEventURLs – the extra capitalisation in the model name throws it off course.
The moral of the story? Be sparing with your capital letters in model names.