TypeError: can't convert nil into Integer

I am getting this error with a very simple model and schema. The record saves even though an error has been generated. I have tried this with both postgres and mysql.

Here is the model.

class Person < ActiveRecord::Base end

Here is a transcript..

p = Person.new => #<Person id: nil, hash: nil, first_name: nil, last_name: nil, middle_name: nil, date_of_birth: nil, date_of_death: nil, age: nil, gender: nil, email: nil, state: 0, status: 0, created_at: nil, updated_at: nil> ree-1.8.7-2011.03 :002 > p.first_name = 'tim' => "tim" ree-1.8.7-2011.03 :003 > p.save TypeError: can't convert nil into Integer   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:322:in `uniq'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:322:in `commit_transaction_records'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:165:in `transaction'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `save'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'   from /home/tim/.rvm/gems/ree-1.8.7-2011.03/gems/activerecord-3.0.5/lib/active_record/transactions.rb:239:in `save'   from (irb):3 ree-1.8.7-2011.03 :004 > Person.all => [#<Person id: 1, hash: nil, first_name: "tim", last_name: nil, middle_name: nil, date_of_birth: nil, date_of_death: nil, age: nil, gender: nil, email: nil, state: 0, status: 0, created_at: "2011-05-12 03:39:33", updated_at: "2011-05-12 03:39:33">] ree-1.8.7-2011.03 :005 >

Here is the SQL statement generated

INSERT INTO `people` (`date_of_birth`, `updated_at`, `gender`, `date_of_death`, `created_at`, `hash`, `first_name`, `middle_name`, `age`, `status`, `last_name`, `email`, `state`) VALUES (NULL, '2011-05-12 03:39:33', NULL, NULL, '2011-05-12 03:39:33', NULL, 'tim', NULL, NULL, 0, NULL, NULL, 0)

Here is the migration

create_table :people do |t|       t.string :hash       t.string :first_name       t.string :last_name       t.string :middle_name       t.date :date_of_birth       t.date :date_of_death       t.integer :age       t.string :gender       t.string :email       t.integer :state , :default => 0       t.integer :status , :default => 0       t.timestamps     end

    add_index :people, :email     add_index :people, :hash     add_index :people, :state     add_index :people, :status

So there is nothing weird at all.

Very strange

TO `people` (`date_of_birth`, `updated_at`, `gender`,

`date_of_death`, `created_at`, `hash`, `first_name`, `middle_name`, `age`, `status`, `last_name`, `email`, `state`) VALUES (NULL, '2011-05-12 03:39:33', NULL, NULL, '2011-05-12 03:39:33', NULL, 'tim', NULL, NULL, 0, NULL, NULL, 0)

Here is the migration

create_table :people do |t| t.string :hash t.string :first_name t.string :last_name t.string :middle_name t.date :date_of_birth t.date :date_of_death t.integer :age t.string :gender t.string :email t.integer :state , :default => 0 t.integer :status , :default => 0 t.timestamps end

add\_index :people, :email
add\_index :people, :hash
add\_index :people, :state
add\_index :people, :status

So there is nothing weird at all.

Having a column called hash is weird - Object has a method called hash, which your attribute will shadow, which could mess up trying to call uniq on an array containing objects of your class

Fred

Having a column called hash is weird - Object has a method called hash, which your attribute will shadow, which could mess up trying to call uniq on an array containing objects of your class

Fred

That was the case. Can't have a column called hash in your models. You trip on the magic.