Summary
I recently was working on adding a migration to an existing table that I'd created as a model (via script/generate). Though it was coded right, the syntax was correct, etc., I received this error:
-- add_column(:users, :industry_id, :int)
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
I later discovered through some googling and some work that :int won't work for migrations generated through script/generate migration, but it will work for script/generate/model.
Another Way to Explain It
I started off by creating my user object:
[user@host]# script/generate model user
Of course the user model was generated. I then populated the user module, using data types like :int, for example.
(Excerpt)
class CreateUsers < ActiveRecord::Migration
def self.up
create_table "users", :force => true do |t|
# ...
t.column :num_ratings, :int
t.column :total_stars, :int
# ...
end
end
def self.down
drop_table "users"
end
end
rake db:migrate did this absolutely fine. Note specifically the use of :int, NOT :integer.
Later in the development process, I decided I wanted to add a foreign key to this user, called "industry_id" that would reference the ID of the industry in which the user worked. So naturally, I did:
[user@host]# script/generate migration add_industry_to_user
Now this is where things get interesting. My migration source looked like this:
class AddIndustryToUser < ActiveRecord::Migration
def self.up
add_column :users, :industry_id, :int
# Foreign Key
execute("alter table users add constraint fk_user_industry foreign key(industry_id) references industries(id)")
end
def self.down
# Remove the FK
#execute("alter table users drop foreign key fk_user_industry")
remove_column :users, :industry_id
end
end
But, running rake db:migrate gave me no love. In fact, it beat me like a red-headed step-child:
-- add_column(:users, :industry_id, :int)
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
Through some googling, I discovered that :int is NOT mapped for a standard migration, but only for migrations generated through script/generate model. You have to use the full-out :integer.
I know of at least this discrepancy, but there could be more. As I'm pretty new to rails, I don't know how to look for those (nor do I really want to - I just want to get this app developed!), so I figured it best to let you guys know.
DISCLAIMER: I'm new to rails. I did check the existing ticket pages and saw nothing aptly named that sounded like this issue, so I'm submitting a new ticket. This is NOT an un-fixable problem that you have to diagnose, it's just a developer oversight (easy to happen on such a large project) that should be easily fixable, in theory.