Ar-Extensions & FasterCSV

1. contactx = Array.new
2. if params[:dump][:dump]
3. FasterCSV.parse(params[:dump][:dump].read.chop, {:headers =>
true, :skip_blanks => true}) do |row|
4. contactx << Contact.new(
5. :FirstName => row["First Name"],
6. :LastName => row["Last Name"],
7. :email => row["Email Address"]
8. )
9. end
10. Contact.import contactx

This code works up to line 4. I get the error: "Array can't be coerced
into Fixnum"

If I change "contactx << Contact.new(..." to "Contact.create(..." so
that it uses active record to create the records, it works. So the
problem is with either line 4 or 10.

I'm stuck right now. Any ideas?

I don't have any ideas based on what you've provided so far. Can you
show the Contact class? Can you show the full stack trace?

Craig

The Contact class is empty:

   class Contact < ActiveRecord::Base
   end

BTW, I already have require 'FasterCSV' & require 'ar-extensions' in
the environment.rb file.

Here is the Full Stack Trace:

C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:272:in `+'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:272:in `import_without_validations_or_callbacks'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:262:in `each'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:262:in `import_without_validations_or_callbacks'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:243:in `import_with_validations'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.7.0/lib/ar-
extensions/import.rb:202:in `import'
app/controllers/test_controller.rb:14:in `index'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:1158:in `send'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:1158:in `perform_action_without_filters'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:697:in `call_filters'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:689:in `perform_action_without_benchmark'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/rescue.rb:199:in `perform_action_without_caching'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/caching.rb:678:in `perform_action'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/connection_adapters/abstract/query_cache.rb:33:in
`cache'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/query_cache.rb:8:in `cache'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/caching.rb:677:in `perform_action'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:524:in `send'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:524:in `process_without_filters'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:685:in
`process_without_session_management_support'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/session_management.rb:123:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:388:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:171:in `handle_request'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:115:in `dispatch'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:126:in `dispatch_cgi'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:9:in `dispatch'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/rails.rb:76:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/rails.rb:74:in `synchronize'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/rails.rb:74:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:159:in `process_client'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:158:in `each'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:158:in `process_client'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:285:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:285:in `initialize'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:285:in `new'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:285:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:268:in `initialize'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:268:in `new'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel.rb:268:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/configurator.rb:282:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/configurator.rb:281:in `each'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/configurator.rb:281:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
bin/mongrel_rails:128:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
lib/mongrel/command.rb:212:in `run'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.3-x86-mswin32/
bin/mongrel_rails:281
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:489:in `load'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:489:in `load'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:342:in `new_constants_in'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:489:in `load'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/
servers/mongrel.rb:64
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:342:in `new_constants_in'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/
server.rb:39
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
script/server:3
-e:2:in `load'
-e:2

The reason it is empty is because I'm just doing a test on this Ar-
Extensions + FasterCSV combination.

Below is what the development.log spits out. Everything looks normal
until WARNING: Can't mass-assign these protected attributes: id.

Hmm. Mabye you shouldn't be importing actual AR instances? Have you
read this article?

http://www.jobwd.com/article/show/31

In it, the import is done by specifying matched arrays of columns and
values (shown near the end just before the comments).

Note that the docs for ar-extensions are still down (I notified the
author awhile ago, but he's been busy preparing for RailsConf).

Craig

Craig,

I've tried that as well... and it still gives the same error. I
suspect it has to do with the NULL value being passed in for the id
column (based on looking at the log). However, I am not doing anything
to specify it so I don't know why it's doing that.

BTW, Thank you for your effort so far

So you've tried something like this and still see the same problem?

if params[:dump][:dump]
  columns = [:FirstName, :LastName, :email]
  values = []
  FasterCSV.parse(params[:dump][:dump].read.chop, {:headers =>true,
:skip_blanks => true}) do |row|
    values << [ row["First Name"], row["Last Name"], row["Email Address"] ]
  end
  Contact.import columns, values
end

Craig

Yes. Infact, I had it practical identical to that

What about versions of Ruby, Rails, ar-extensions, FasterCSV? What
database and version of it are you using? What platform are you
running?

Craig

Ruby 1.8, Rails 2.0.2, AR 0.7.0, FasterCSV is the latest version, Win
XP, InstantRails, SQLite

I don't have experience on Windows with InstantRails and SQLite. Are
you able to try a different database, such as mysql? Are you able to
try with Rails outside of InstantRails?

AR-extensions says its compatible with SQLite and instantrails is the
same as regular rails so it wouldn't have any effect on the problem.

Ok I've simplified the code to try and identify what the heck is going
on. I took out every except:

      contactx = [ Contact.new( :first_name =>"First 1", :last_name
=>"Last 1", :email_address => "Email 1"),
                               Contact.new( :first_name =>"First
2", :last_name =>"Last 2", :email_address => "Email 2")]
      Contact.import contactx

And it still won't work

It's a bug in ar-extensions. (and looking at the code it would seem
they were relying on something that isn't documented. The trunk
version looks to have fixed this.

Fred

The trunk version? How do I get that?

I just installed ar-ext from http://arext.rubyforge.org/svn/trunk/ar-extensions/
and that didn't change anything. Any other ideas?

The trunk version? How do I get that?

It's linked from the rubyforge project page.

Fred

I just installed ar-ext from http://arext.rubyforge.org/svn/trunk/ar-extensions/
and that didn't change anything. Any other ideas?