Help: Why does this fail

Trying to load a db table with sample data…

This seems like a very simple and straitforward task.

require ‘rubygems’
require ‘active_record’

class Rich < ActiveRecord::Base
end

class Corp < ActiveRecord::Base
end

ActiveRecord::Base.establish_connection(
    :adapter => "mysql",
    :host => "localhost",
    :username => "root",
    :password => "",
    :database => "foo_development"
)

@x = Rich.find(:all)

@x.each {
|a |
n = Corp.new
n[“name”] = a[“name”]
n[“city”] = a[“city”]
n[“state”] = a[“state”]
n[“zip”] = a[“zip”]

 n.save;

}

Just before executing the n.save line…

(rdb:1) p n
#<Corp:0x2a0bbe4 @attributes={“city”=>“San Jose”, “name”=>“Wire Inc”,
“status”=>nil, “zip”=>“95131-1873”, “address2”=>nil, “cf”=>nil,
“notes”=>nil, “market”=>nil, “isp”=>nil, “domain_name”=>nil,
“fax”=>nil, “phone”=>nil, “circuit_type”=>nil, “address”=>nil,
“source”=>nil, “state”=>“CA”}, @new_record=true>
(rdb:1) n

Error:

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2068:
can't clone NilClass' (TypeError) from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2037:inquoted_column_names’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1741:in
create_without_callbacks' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:265:increate_without_timestamps’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/timestamp.rb:30:in
create' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1718:increate_or_update_without_callbacks’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:253:in
create_or_update' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:insave_without_validation’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:736:in
save_without_transactions' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:insave’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in
transaction' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:intransaction’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:in
transaction' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:insave’
from populateCorp.rb:28
from populateCorp.rb:20:in `each’
from populateCorp.rb:20
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2068:
value.clone

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs