Rails项目启动报错:加载delayed_job_active_record gem失败求助
Rails 3.2.9启动失败:delayed_job_active_record加载报错(undefined method `yaml_as')
问题重现
执行rails server -e development启动开发服务器时,触发Bundler::GemRequireError,核心错误为加载delayed_job_active_record gem失败,提示:
undefined method `yaml_as' for ActiveRecord::Base:Class Did you mean? yaml_tag
已安装Node.js,但问题未解决,完整报错堆栈如下:
/home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/values/time_zone.rb:270: warning: circular argument reference - now Traceback (most recent call last): 13: from script/rails:6:in `<main>' 12: from script/rails:6:in `require' 11: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in `<top (required)>' 10: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in `tap' 9: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in `block in <top (required)>' 8: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in `require' 7: from /home/deployer/apps/isg/20220314091757/config/application.rb:15:in `<top (required)>' 6: from /usr/lib/ruby/vendor_ruby/bundler.rb:114:in `require' 5: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in `require' 4: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in `each' 3: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in `block in require' 2: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in `each' 1: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:80:in `block (2 levels) in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:84:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'delayed_job_active_record'. (Bundler::GemRequireError) Gem Load Error is: undefined method `yaml_as' for ActiveRecord::Base:Class Did you mean? yaml_tag Backtrace for gem load error is: /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:4:in `<class:Base>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:3:in `<module:ActiveRecord>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:2:in `<top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `block in require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:236:in `load_dependency' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/worker.rb:64:in `backend=' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job_active_record-4.0.2/lib/delayed_job_active_record.rb:5:in `<top (required)>' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:81:in `require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:81:in `block (2 levels) in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in `each' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in `block in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in `each' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in `require' /usr/lib/ruby/vendor_ruby/bundler.rb:114:in `require' /home/deployer/apps/isg/20220314091757/config/application.rb:15:in `<top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in `require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in `block in <top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in `tap' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in `<top (required)>' script/rails:6:in `require' script/rails:6:in `<main>' Bundler Error Backtrace:
原因分析
Rails 3.2.x版本的ActiveRecord::Base中,yaml_as方法已被移除,替换为yaml_tag。而当前使用的delayed_job(4.0.4)和delayed_job_active_record(4.0.2)是针对更高版本Rails开发的,代码中仍调用了已废弃的yaml_as方法,导致版本不兼容报错。
解决方案
方案一:降级delayed_job到兼容版本
修改项目的Gemfile,指定与Rails 3.2.x兼容的delayed_job系列版本(3.x分支):
gem 'delayed_job', '~> 3.0.5' gem 'delayed_job_active_record', '~> 3.0.5'
执行以下命令更新依赖:
bundle install
完成后重新启动服务器即可。
方案二:临时修改gem代码(不推荐长期使用)
找到delayed_job的序列化文件,将yaml_as替换为yaml_tag:
- 定位文件:
vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb - 修改第4行代码:
原代码:
修改为:yaml_as "tag:ruby.yaml.org,2002:ActiveRecord::Base"yaml_tag "tag:ruby.yaml.org,2002:ActiveRecord::Base"
注意:此方法在执行bundle update后会失效,仅作为临时应急方案,优先推荐方案一。
内容的提问来源于stack exchange,提问作者tarikcn34
相关产品推荐
相关产品推荐

