0

我们使用 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 来在需要时自动加载模型,还是上面的设置脚本中有其他可能导致这种行为的东西?

4

1 回答 1

0

这种警告表明您使用的库与 ruby​​ 1.9+ 不完全兼容。

根据我的经验,只要产生这些结果的库对重新初始化不可知,这些警告就不会对程序结果产生任何影响。

但是,应该有更新,至少对于 activerecord 和 activesupport(到 3.0.6)。

于 2011-04-13T14:52:47.303 回答