RoR is fun !
Unless... you encounter some problem that google cannot explain ;)
So recently I've got some strange TypeError (can't convert String into Integer) in .rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:319 when doing .save on my object.
Records were successfully saved into database but this error stopped app from execution.
I've investigated this a little, trying to find this mysterious String which ruby wants to convert to Integer.
Let me tell you a little about my object I want to save.
class CreateSites < ActiveRecord::Migration
def self.up
create_table :sites do |t|
t.integer :id, :null => false
t.text :title, :null => false
t.column :hash, :'varchar(32)', :null => false
t.text :description, :null => false
t.column :user_id, 'integer references users(id)', :null => false
t.timestamps
end
end
def self.down
drop_table :sites
end
end
This is my migration file, clean and simple. Nothing can be wrong here, well that's a lie ;)
Ruby was complaining about my hash field, why ? Because every object in ruby has a hash method, which is used for eg. in comparison when doing Array.uniq (which is done in rails). Then when I created this migration, rails created a Class which was overwriting hash method (which returns int) to return the db value (which is string).
Sooooooo not cool ! Why this isn't documented anywhere ? Shame on you rails developers ! It should be red in migration guide - DO NOT USE "HASH" AS A FIELD NAME, IT WILL BREAK YOUR APP