开发者

Using ActiveRecord models from Rails app in a Sinatra app produces multiple warnings

开发者 https://www.devze.com 2023-02-17 07:00 出处:网络
We are using Sinatra to provide HTTP API access to our service, while most of the user-facing functionality uses Rails 2.3.8. Both the Sinatra and the Rails application share the same ActiveRecord mod

We are using Sinatra to provide HTTP API access to our service, while most of the user-facing functionality uses Rails 2.3.8. Both the Sinatra and the Rails application share the same ActiveRecord models, defined in the RAILS_ROOT/app/models directory.

In an setup script for the Sinatra application, all of the models are loaded using ActiveSupport::Dependencies and the database connection is initialized:

require 'active_support'
relative_load_paths = %w(app/models)
::ActiveSupport::Dependencies.load_paths = relative_load_paths.map { |path|
  File.expand_path(path, RAILS_ROOT)
}

require 'active_record'
config_path     = File.expand_path('config/database.yml', RAILS_ROOT)
all_envs_config = YAML.load(File.read(config_path)) 
config          = all_envs_config[env.to_s]
::ActiveRecord::Base.establish_connection(config)

The above alone does not produce any warnings, but whenever any of our models is used from the API side (e.g. in tests), multiple warnings will be output to the console:

/path/to/ruby/gems/activesupport-2.3.8/lib/active_support/vendor.rb:32: warning: redefine normalize_translation_keys
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/validations.rb:393: warning: `*' interpreted as argument prefix
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/validations.rb:29: warning: method redefined; discarding old message
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/dirty.rb:40: warning: `*' interpreted as argument prefix
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:228: warning: `*' interpreted as argument prefix
/path/to/ruby/gems/sinatra-1.2.0/lib/sinatra/base.rb:1096: warning: method redefined; discarding old options
/path/to/ruby/gems/shoulda-2.10.3/lib/shoulda/context.rb:4: warning: method redefined; discarding old contexts
/path/to/ruby/gems/shoulda-2.10.3/lib/shoulda/context.rb:330: warning: method redefined; discarding old subject_block
/path/to/ruby/gems/shoulda-2.10.3/lib/shoulda/proc_extensions.rb:4: warning: method redefined; discarding old bind
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/integration.rb:99: warning: `*' interpreted as argument prefix
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:90: warning: method redefined; discarding old path
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:397: warning: me开发者_高级运维thod redefined; discarding old get
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:402: warning: method redefined; discarding old post
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:407: warning: method redefined; discarding old put
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:412: warning: method redefined; discarding old delete
/path/to/ruby/gems/actionpack-2.3.8/lib/action_controller/test_process.rb:417: warning: method redefined; discarding old head
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/reflection.rb:261: warning: instance variable @collection not initialized
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/reflection.rb:261: warning: instance variable @collection not initialized
/path/to/ruby/gems/activerecord-2.3.8/lib/active_record/reflection.rb:261: warning: instance variable @collection not initialized
...

Is this because of our use of ActiveSupport::Dependencies to automatically load the models whenever needed, or is there something else in the setup script above that might be causing this behavior?


That kind of warnings indicate that you are using a library not fully compatible with ruby 1.9+.

From what I experienced, these warnings don't make any impact on the program results as long as the libraries that are producing these results are agnostic to reinitializing.

However, there should be updates, at least for activerecord and activesupport (to 3.0.6).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号