Problem running ruby script with rails42

This very simple script was running in 4.1.6

require ‘.\config\config.rb’

require ‘.\models\transazioni.rb’

Transazioni.where(isprocessed: ‘N’).each do |tr|

puts tr.id_transazione + ' ' + tr.transdate.to_s + ' ' + tr.idmaximo.to_s + ' ' + tr.codsap

end

exit

database.yml:

DATA SOURCE

datasource:

adapter: oracle_enhanced

url: jdbc:oracle:thin:@x.x.x.x:1521:xxxx

username: xxx

password: xxx

config.rb:

require ‘rubygems’

require ‘active_record’

require ‘yaml’

require ‘logger’

require ‘C:/jrails4/jruby/lib/ojdbc6.jar’

@config_db = YAML::load(File.open(“config/database.yml”))

$src_db = @config_db[“datasource”]

Now with 4.2.0 I got this error:

C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:587 warning: already initialized constant Reference

C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:37 warning: already initialized constant MAPPING

C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:186 warning: already initialized constant UTC_OFFSET_WITH_COLON

C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:187 warning: already initialized constant UTC_OFFSET_WITHOUT_COLON

C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:40 warning: already initialized constant BLACKLISTED_ARRAY_METHODS

ActiveSupport::Concern::MultipleIncludedBlocks: Cannot define multiple ‘included’ blocks for a Concern

included at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/concern.rb:126

ClassSpecificRelation at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:54

Delegation at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:51

ActiveRecord at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:6

(root) at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:5

require at org/jruby/RubyKernel.java:1071

require at C:/jrails4_update/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:69

require at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274

load_dependency at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240

require at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274

(root) at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/base.rb:1

(root) at C:/jrails4_update/jruby/lib/ruby/gems/shared/gems/activerecord-4.2.0/lib/active_record/base.rb:23

require at org/jruby/RubyKernel.java:1071

require at C:/jrails4_update/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:121

(root) at transcont.rb:4

Do I have to modify something or it’s a bug?

Tnx

Fabio

P.S.

transazioni.rb (model)

class Transazioni < ActiveRecord::Base

self.table_name = ‘TRANSAZIONI_CONTABILI’

establish_connection($src_db)

end

My guess would be that something is getting loaded twice. I suggest
taking out all the requires and putting them back till you get the
problem. That may help you to home in on it.

Colin

My guess would be that something is getting loaded twice. I suggest

taking out all the requires and putting them back till you get the

problem. That may help you to home in on it.

Yes, it seems some constant is already initialized.

I tried removing the includes, but they are all needed…

The natural way to run a script in a Rails environment is to use the runner command.

  1. Remove manual requires.

  2. bin/rails runner my_script.rb

The point is which is the one you add that causes this particular
error to occur? Once you find that then leave it in and take the
others out, then add them back in one at a time to find out which is
the one that adds the second initialisation.

Colin

The point is which is the one you add that causes this particular

error to occur? Once you find that then leave it in and take the

others out, then add them back in one at a time to find out which is

the one that adds the second initialisation.

I can remove require ‘rubygems’

But if I remove the next require line which requires activerecord then I got a name error which says that active record is not defined

Same thing if I remove require yaml… and if I require active record and yaml then the error arise, even if I don’t require logger…

does not seems to work under jruby… rails batch seems to not support the runner option…

See: https://github.com/jruby/jruby/issues/2286

I suspect that's your issue.

HTH!

Did you find out how to fix this?

Yes, it was a jruby problem, I solved with jruby 1.7.22

:wink: