Ruby 如何判断不同的 rails 版本应该用哪个版本的 gem

yyyuezhi · 2023年09月12日 · 最后由 yyyuezhi 回复于 2023年09月19日 · 966 次阅读

我用的 rails 版本是 6.0.5.1,gem 引用了 gem 'database_cleaner', '~> 1.5.3',gem 'rspec-rails', '~> 5.0.0',用 rspec 命令进行单元测试出现一个错误

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload ActionText::ContentHelper, for example,
the expected changes won't be reflected in that stale Module object.

These autoloaded constants have been unloaded.

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <top (required)> at /home/henry/projects/master_actions/config/environment.rb:5)

An error occurred in a `before(:suite)` hook.
Failure/Error: DatabaseCleaner.clean_with(:truncation)

NoMethodError:
  undefined method `schema_migrations_table_name' for ActiveRecord::Migrator:Class
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/active_record/truncation.rb:260:in `migration_storage_names'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/generic/truncation.rb:14:in `initialize'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:39:in `new'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:39:in `create_strategy'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:43:in `clean_with'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:91:in `block in clean_with'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:91:in `each'
# /usr/share/rvm/gems/ruby-2.7.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:91:in `clean_with'
# ./spec/spec_helper.rb:50:in `block (2 levels) in <top (required)>'

Finished in 0.02272 seconds (files took 1.25 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

查了一下,好像是 database_cleaner 的版本不对。那么我如何判断某个版本的 rails 该用哪个版本的 gem 呢?

https://github.com/DatabaseCleaner/database_cleaner/issues/476 看情况你这老项目的 rails 版本是 5.1 之前的

所以大家都不想升级依赖,只要能跑就不动,嘿嘿。

正常流程:

  1. 根据报错问 GPT
  2. 根据报错搜 Google
  3. 搜不到提 issue,但作者回不回复你看运气
  4. 要么回滚,要么自己翻源码

基本全凭踩坑。

可以看 gem version 的 release time,然后跟 rails version 的 release time 比较。必须要 rails version release time < gem version release time,如果 gem 的 Readme 有说明 support Rails versions 最好。

ken 回复

oh,竟然是这样😂

lehf 回复

后来是换了个版本就好了,我就是想着我怎么才能直接装正确的版本😆

需要 登录 后方可回复, 如果你还没有账号请 注册新账号