Skip to content
dgilperez edited this page Mar 11, 2013 · 7 revisions

There are essentially two ways to require activerecord-import:

  • Using Bundler (default Rails 3 app uses this)
  • Not using Bundler

These instructions only work for activerecord-import 0.2.0 or higher. If you have a version before 0.2.0 then upgrade.

Using Bundler

There are two ways you can use Bundler. First, the Rails 3 way.

The Rails 3 Way – autoloading gem dependencies

Rails 3 by default autoloads your gem dependencies (thanks to config/application.rb). When this happens you only need to make sure activerecord-import is specified as a gem dependency in your Gemfile.

  gem "activerecord-import", ">= 0.2.0"

You might however want to load activerecord-import on your own without the help of bundler. You can do this by telling bundler not to auto-require it.

  gem 'activerecord-import', '~> 0.3.1', :require => false

This does Not work if we just do require ‘activerecord-import’ in the file using the gem.
However, we still want to use the gem.
This can still be done using following snippet for manual include in your file:

  require "activerecord-import/base"
  ActiveRecord::Import.require_adapter('mysql2')

The mysql2 is your adapter. Could be dynamically got from ActiveRecord::Base.configurations.

This allows it be used in a more isolated fashion, impacting only the file that it is needed.

Not autoloading

If your gem dependencies aren’t autoloaded then simply require activerecord-import after activerecord has been loaded, ie:

require 'active_record'
require 'activerecord-import'

That’s it.