尝试加载 gem 'devise. ActiveSupport:持续时间不能强制转换为整数
Posted
技术标签:
【中文标题】尝试加载 gem \'devise. ActiveSupport:持续时间不能强制转换为整数【英文标题】:Error while trying to load the gem 'devise. ActiveSupport: Duration can't be coerced into Integer尝试加载 gem 'devise. ActiveSupport:持续时间不能强制转换为整数 【发布时间】:2017-06-01 20:52:41 【问题描述】:我一直在尝试修复此错误一段时间,但没有发现任何帮助,请帮助。
我正在尝试使用设计 gem 在我的 ROR 应用程序上运行迁移。但是我从 ActiveSupport 中得到一个错误“不能将持续时间强制转换为整数”。这发生了然后我尝试运行:
rake db:migrate
我不确定这是否与迁移代码有关,或者是否与新设计版本有关。 这是错误信息:
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/thread_safe-0.3.5/lib/thread_safe/cache.rb:155: warning: constant ::Fixnum is deprecated
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'devise'.
Gem Load Error is: ActiveSupport::Duration can't be coerced into Integer
Backtrace for gem load error is:
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `*'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `days'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:55:in `weeks'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:121:in `<module:Devise>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:9:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:687:in `raw_load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:96:in `block in load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:95:in `load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:79:in `block in run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/bin/rake:22:in `load'
/home/sam/.rbenv/versions/2.4.0/bin/rake:22:in `<main>'
Bundler Error Backtrace:
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
TypeError: ActiveSupport::Duration can't be coerced into Integer
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
我正在使用:
ruby '2.4.0'
gem 'rails', '4.1.1'
gem 'devise', '~> 4.2.0'
请告诉我如何才能使这个问题变得更好。 问候。
编辑: 这是迁移文件之一:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.datetime :remember_created_at
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
t.timestamps
end
end
end
另外,在尝试时 bundle exec rake db:migrate 我得到的几乎一样:
bundle exec rake db:migrate
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/thread_safe-0.3.5/lib/thread_safe/cache.rb:155: warning: constant ::Fixnum is deprecated
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'devise'.
Gem Load Error is: ActiveSupport::Duration can't be coerced into Integer
Backtrace for gem load error is:
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `*'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `days'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:55:in `weeks'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:121:in `<module:Devise>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:9:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/aplicaciones/pess/Rakefile:4:in `require'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:687:in `raw_load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:96:in `block in load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:95:in `load_rakefile'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:79:in `block in run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/bin/rake:22:in `load'
/home/sam/.rbenv/versions/2.4.0/bin/rake:22:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
Bundler Error Backtrace:
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/aplicaciones/pess/Rakefile:4:in `require'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
TypeError: ActiveSupport::Duration can't be coerced into Integer
/home/sam/aplicaciones/pess/config/application.rb:7:in `<top (required)>'
/home/sam/aplicaciones/pess/Rakefile:4:in `require'
/home/sam/aplicaciones/pess/Rakefile:4:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/sam/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
(See full trace by running task with --trace)
【问题讨论】:
您是在运行bundle exec rake db:migrate
还是只运行rake db:migrate
?
您可以发布迁移文件吗?
@virtuexru 我只是在运行 rake db:migrate,但在尝试 bundle exec rake db:migrate 时得到相同的输出。
@RockwellRice,当然,我会编辑帖子;)
【参考方案1】:
尝试运行bundle exec rake db:migrate
并让我知道输出。如果没有前面的bundle exec
,它将不知道要使用哪些gem,并且将使用本机(系统)gem。
还可以尝试运行bundle update
,然后重新运行迁移。
【讨论】:
感谢您回答 Virtuexru,我尝试了 bundle update,然后 bundle exec rake db:migrate 但输出几乎相同。我编辑了帖子。 你能删除 gemfile 中devise
的版本吗?意思是:gem 'devise' 并在运行之前先运行bundle
` bundle exec rake db:migrate`
你也安装了nodejs
吗?你可能需要它。
virtuexru,我完全按照你说的做了,但它并没有改变输出。
嗯;我会想象devise
gem 有问题。我在网上找不到与此错误相关的任何其他内容。对不起:(最坏的情况你可以在这里打开一个问题:github.com/plataformatec/devise/issues【参考方案2】:
今天在尝试将旧应用升级到 rails 4 时遇到了问题。
问题是 ruby 版本(2.4.0)而不是自己设计的。考虑使用 ruby-2.3.3 或更低版本,它会像魅力一样工作!
【讨论】:
是的,我就是这么做的。 ruby 2.4 不支持 Rails 3.2,所以必须使用较低版本。 它对我有用!谢谢。我必须将我的 Rails 4 升级到 Rails 5。但是,我使用的是 ruby-2.3.3。 对于未来的用户,请考虑使用 ruby-2.3.4 或更高版本以避免 2.3.3 及之前版本中存在巨大的漏洞 这种不可靠的依赖管理在 Ruby 生态系统中似乎特别明显......【参考方案3】:我昨天在让 rails 3.2 和 ruby 2.4 一起工作时遇到了这个问题。我通过猴子修补问题所在的 activesupport 库来修复它。
解决方法:切换乘法顺序,把Duration放在第一位
在 Rails 初始化程序中包含以下代码:
# pulled from https://github.com/rails/rails/blob/v3.2.22.5/activesupport/lib/active_support/core_ext/numeric/time.rb
class Numeric
def days
ActiveSupport::Duration.new(24.hours * self, [[:days, self]])
end
alias :day :days
def weeks
ActiveSupport::Duration.new(7.days * self, [[:days, self * 7]])
end
alias :week :weeks
def fortnights
ActiveSupport::Duration.new(2.weeks * self, [[:days, self * 14]])
end
alias :fortnight :fortnights
end
# pulled from https://github.com/rails/rails/blob/v3.2.22.5/activesupport/lib/active_support/core_ext/integer/time.rb
class Integer
def months
ActiveSupport::Duration.new(30.days * self, [[:months, self]])
end
alias :month :months
def years
ActiveSupport::Duration.new(365.25.days * self, [[:years, self]])
end
alias :year :years
end
【讨论】:
有了这个答案和this one,你就是我今天的英雄。 与@JetBlue 相同 很抱歉 - 我应该如何处理这个?我在config/initializers
中创建了一个新文件并将其放在那里,但它不起作用
我需要将 Integer 和 Duration 类放在哪里? @Thomas Dziedzic
嘿@mychemicalro,我把它修好了:udemy.com/learn-ruby-on-rails-from-scratch/learn/v4/questions/…【参考方案4】:
切换到至少 5.0.2 的 rails 版本,删除您的 Gemfile.lock 并运行 bundle update
。
5.2.0
是最新的,发布于 18 年 4 月 9 日..
【讨论】:
为什么是 5.0.2?1.day
在 Ruby 2.4.2 和 Rails 4.2.10 上工作得很好。最初的发布者在 Rails 4.1.1 上遇到了这个错误。所以问题在这些版本之间的某个时候得到了修复。
安全,并在将来版本变得更难更新时帮助自己。如果 OP 引用的是过时的 gem,并且猴子修补系统库,它可能会在某一天回来,而不是在版本可用时更新版本。
我不明白。为什么 5.0.2 足够安全而 5.0.1 不够安全?似乎如果问题是特定于 Rails 版本的,那么说明问题是在哪个版本中修复的而不是说问题是在哪个版本中修复的,这对于某些未指定的标准来说也足够安全。
在任何项目中使用最新的稳定版通常是一种更好的方法。这样做可以让您在应用程序中减少安全漏洞的几率更大。这是因为较新的版本有补丁或说明在软件包的整个生命周期中发现的任何漏洞。因此,正确的方法不涉及修改系统库和创建更多漏洞,而是从长远来看更新您的系统并获得这些额外的好处。我不确定您关于 Rails 5.01 与 5.0.2 中的错误修复的问题的具体内容,也许更好地提出一个新问题。【参考方案5】:
今天,我遇到了同样的问题,但版本不同,我的 ruby 版本 2.4.2 和 rails 版本 4.0.13。
我遇到了同样的错误,现在修复了。感谢@thomas-dziedzic。
解决办法是:
-
我从
https://github.com/rails/rails/commit/32f215c3014e580e512db5e8772d0deadf3d6497
我根据这个链接编辑然后编辑https://github.com/rails/rails/blob/32f215c3014e580e512db5e8772d0deadf3d6497/activesupport/lib/active_support/core_ext/numeric/conversions.rb
当我编辑 active_support 时,问题已解决。
【讨论】:
【参考方案6】:实际上我在这些条件下尝试安装和运行activeadmin
时遇到了同样的问题:
导轨 3.2.22.5 红宝石 2.3.8
solution by Thomas 对我来说并不完全有效,因为在应用程序启动之前调用了 ActiveSupport。
对我有用的解决方案:
我将 ActiveSupport 提取为单独的 gem,forked it,应用了 Thomas 的上述补丁并进行了一些额外的修复,以便它可以作为 gem 在 Rails 之外运行。
我还在activeadmin
之前添加了这个gem,所以当需要Duration
时,它会运行我的修补版本:
# Gemfile extract
gem 'activesupport', :git => 'git://github.com/beshur/activesupport.git'
gem 'activeadmin', '0.6.6'
然后我将我的 heroku 应用程序切换为使用 stack heroku-16 (more on stacks here),但它仍然需要 ruby 2.3.8。
希望它以后对某人有所帮助,尽管它已经过时了。
【讨论】:
【参考方案7】:我在迁移旧版应用时遇到了同样的问题。将 Rails 升级到 4.2 后,您可以将 Ruby 版本升级到 2.4,然后继续使用 Rails 和 Ruby 进行升级。
我建议在你的 Gemfile 中使用 bootboot gem,这样你就可以只用一个环境变量在新旧 Rails 版本中测试运行规范,而无需切换 Git 分支。
【讨论】:
以上是关于尝试加载 gem 'devise. ActiveSupport:持续时间不能强制转换为整数的主要内容,如果未能解决你的问题,请参考以下文章
Rails, Devise, Postmark Gem - 使用邮戳模板设计邮件
如何使用 Devise gem 生成的 User 模型生成的 UserID 作为其他模型的外键?
Monkey patching Devise(或任何 Rails gem)