如果我正在运行sidekiq,我遇到以下异常:
可以看出,尽管我在我的应用程序中没有使用require和require_relative,但是由于一些奇怪的原因,date_at_relative_to_now类需要两次。我完全依赖rails rails autoload功能。
赞赏任何想法或帮助
→ sidekiq
loading...
loading...
Cannot define multiple 'included' blocks for a Concern
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/concern.rb:126:in `included'
/Users/boti/Rails/checker/app/exhibits/concerns/date_at_relative_to_now.rb:6:in `<module:DateAtRelativeToNow>'
/Users/boti/Rails/checker/app/exhibits/concerns/date_at_relative_to_now.rb:3:in `<top (required)>'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:317:in `depend_on'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:233:in `require_dependency'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:471:in `each'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:471:in `block in eager_load!'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:469:in `each'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:469:in `eager_load!'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/engine.rb:346:in `eager_load!'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/application/finisher.rb:56:in `each'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/application/finisher.rb:56:in `block in <module:Finisher>'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:345:in `each'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:345:in `call'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
/Users/boti/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
/Users/boti/Rails/checker/config/environment.rb:5:in `<top (required)>'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:241:in `boot_system'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:50:in `run'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/gems/sidekiq-3.3.4/bin/sidekiq:8:in `<top (required)>'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/bin/sidekiq:23:in `load'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/bin/sidekiq:23:in `<main>'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/bin/ruby_executable_hooks:15:in `eval'
/Users/boti/.rvm/gems/ruby-2.2.0@checker/bin/ruby_executable_hooks:15:in `<main>'
ruby-2.2.0@checker:boti:~/Rails/checker git:active_job_exercise
→
date_at_relative_to_now.rb的代码:
module DateAtRelativeToNow
puts 'loading...'
extend ActiveSupport::Concern
included do
include ActionView::Helpers::DateHelper
end
module ClassMethods
attr_reader :field
def applicable_to?(object, _context)
field = self.field
object.respond_to? field
end
def applicable_to(field)
@field = field
define_method field do
date = __getobj__.send self.__class__.field
if date.present?
"#{distance_of_time_in_words_to_now date} ago"
else
'Never'
end
end
end
end
end
答案 0 :(得分:0)
问题发生时activesupport不支持包含在“包含”块内。
从DateAtRelativeToNow中删除include ActionView::Helpers::DateHelper
行,并将其包含在您需要的模块或类中。
答案 1 :(得分:0)
实施主动支持&#39;包括&#39;功能:
def included(base = nil, &block)
if base.nil?
raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block)
@_included_block = block
else
super
end
end