【问题标题】:Using ActiveRecord models from Rails app in a Sinatra app produces multiple warnings在 Sinatra 应用程序中使用 Rails 应用程序中的 ActiveRecord 模型会产生多个警告
【发布时间】:2011-03-16 07:31:46
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails activerecord sinatra activesupport ruby-on-rails-2


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 2017-09-16
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多