activerecord-import gem NoMethodError: undefined method 'import'

4.9k views Asked by At

I'm trying to use the activerecord-import gem and I've followed the directions on the wiki to a tee but I'm getting a NoMethodError: undefined method 'import' for #<Class:0x8b009b0>. Here's my code (basically the same as the example from the wiki)

    class ExampleCode
      def self.testing
        orders = []
        10.times do |i|
          orders << Order.new(:raw_data => "order #{i}")
        end

        Order.import orders

      end
    end

I call the method like so:

ExampleCode.testing

I've tried on windows, linux, with a sqlite database, a mysql database and still no luck. And I'm certain I have the gem installed:

actionmailer (3.2.6, 3.2.3, 3.2.1, 3.2.0)
actionpack (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activemodel (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord-import (0.2.10)
activerecord-oracle_enhanced-adapter (1.4.1)
activerecord-sqlserver-adapter (3.2.1)....

I even tried to use require (which shouldn't be necessary when the gem is installed. I haven't seen this come up anywhere else so I fear I must to missing something very obvious

2

There are 2 answers

4
rb512 On

You'll have to import active_record and activerecord-import

i.e.

require active_record
require activerecord-import

(as mentioned in the wiki)

The reason being, ruby won't know about it unless you explicitly import those libraries. In case of a rails project, rails imports all gems mentioned in the Gemfile for you.

0
Joel M. On

I ran into the same thing; turns out that for me at least I had the gem included within a test block in the gemfile. make sure that when you include it it's not just limited to one app environment in the gem file.