我们使用 Sinatra 提供对我们服务的 HTTP API 访问,而大多数面向用户的功能使用 Rails 2.3.8。Sinatra 和 Rails 应用程序共享相同的 ActiveRecord 模型,定义在 RAILS_ROOT/app/models 目录中。
在 Sinatra 应用程序的设置脚本中,所有模型都使用加载ActiveSupport::Dependencies
并初始化数据库连接:
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)
仅以上内容不会产生任何警告,但无论何时从 API 端使用我们的任何模型(例如在测试中),都会向控制台输出多个警告:
/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: method 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
...
这是因为我们使用 ActiveSupport::Dependencies 来在需要时自动加载模型,还是上面的设置脚本中有其他可能导致这种行为的东西?