Jay Godse
Jay Godse

Reputation: 15503

How to use ActiveRecord in a JRuby script outside Rails?

I would like to use JRuby to run a script which populates a database. I am already running JRuby/Rails for a Rails server in another application, and this setup works.

I ran the script (foo.rb) fine until I tried to include the ActiveRecord gem in line 2. The code looks like this:

require 'rubygems'
require 'activerecord'
require 'csv'
nf=[]
Dir["./*.*"].each{|f|nf<<f if File::ctime(f) + 600 > Time.now} 
nf.each{|f| p f.inspect}

The error I get looks like this:

unix> jruby --1.9 foo.rb
LoadError: no such file to load -- activerecord
require at org/jruby/RubyKernel.java:1047
require at /home/user/.rbenv/versions/jruby-1.6.4/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
(root) at foo.rb:2

My gem listing includes all of rails 3.0.10, activerecord 3.0.10, activerecord-jdbc-adapter 1.2.0, activerecord-jdbcmysql-adapter 1.2.0.

I also have an environment variable (JRUBY-OPTS=--1.9) which tells JRuby to run version 1.9 when I use Rails.

What am I missing to get JRuby 1.9 to run with ActiveRecord 3.0.10 to hit a MySQL database?

Upvotes: 0

Views: 618

Answers (1)

Robert Brown
Robert Brown

Reputation: 11016

Change your require to:

require 'active_record'

Upvotes: 4

Related Questions