简体   繁体   中英

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?

将您的要求更改为:

require 'active_record'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM