Rake任务同时使用Active Record和View Helpers。

huangapple go评论63阅读模式
英文:

Rake task with both Active Record and View Helpers

问题

I can provide a translation of the code portion you provided:

我想要在我的(Active Record)模型对象的属性上应用ActionView::Helpers::NumberHelper#number_with_precision方法,而这是在Rake任务中。我希望这样能够工作(但是会出错,下面是完整的跟踪信息):

desc '测试辅助程序是否可访问'
task helper_access: :environment do
  include ActionView::Helpers
  puts number_with_precision(Purchase.first.payment, strip_insignificant_zeros: true)
end

Rake任务中似乎可以包含ActionView::Helpers来成功引用#number_with_precision,并且在Rake任务中成功引用我的模型(当不包含ActionView::Helpers时)。但是两者结合在一起,我会收到错误消息。

这是我尝试过的内容。请问能否帮助我一下。

desc '测试辅助程序是否可访问'
task helper_access: :environment do
  include ActionView::Helpers
  puts number_with_precision(10.0, strip_insignificant_zeros: true)
end

$ rake helper_access # 10

desc '测试辅助程序是否可访问'
task helper_access: :environment do
  puts Purchase.first.payment
end

$ rake helper_access # 7000.0

desc '测试辅助程序是否可访问'
task helper_access: :environment do
  include ActionView::Helpers
  puts Purchase.first.payment
end

Please note that I have provided the translation of the code part as requested, excluding the error message details. If you have any specific questions or need further assistance with this code, feel free to ask.

英文:

I would like to apply ActionView::Helpers::NumberHelper#number_with_precision method to an attribute of an object of my (Active Record) model within a Rake task. I’d hoped this would work (but it errors, full trace below):

desc 'test if helpers accessible'
task helper_access: :environment do
  include ActionView::Helpers
  puts number_with_precision(Purchase.first.payment, strip_insignificant_zeros: true)
end

I seem to be able to include ActionView::Helpers within the Rake task to successfully reference #number_with_precision and separately successfully reference my model in the Rake task (when not including ActionView::Helpers). But do both together and I get an error.

Here’s what I’ve tried. Could I have some help please.

desc 'test if helpers accessible'
task helper_access: :environment do
  include ActionView::Helpers
  puts number_with_precision(10.0, strip_insignificant_zeros: true)
end

$ rake helper_access # 10

desc 'test if helpers accessible'
task helper_access: :environment do
  puts Purchase.first.payment
end

$ rake helper_access # 7000.0

desc 'test if helpers accessible'
task helper_access: :environment do
include ActionView::Helpers
puts Purchase.first.payment
end
E, [2023-05-11T10:51:23.426378 #7159] ERROR -- : Exception rescued in listen-run_thread:
ArgumentError: wrong number of arguments (given 1, expected 2..5)
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/actionview-6.1.7/lib/action_view/helpers/form_options_helper.rb:163:in `select'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:317:in `readpartial'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:290:in `read_events'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:263:in `process'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:236:in `block in run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:232:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:232:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/linux.rb:38:in `_run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/base.rb:79:in `block in start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/thread.rb:26:in `rescue_and_log'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/thread.rb:18:in `block in new'
--- Thread.new ---
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/base.rb:75:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/forwardable.rb:238:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/listener.rb:71:in `block in <class:Listener>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:124:in `instance_eval'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:124:in `call'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:105:in `transition_with_callbacks!'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:72:in `transition'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/listener.rb:92:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:109:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:88:in `initialize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:44:in `new'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:44:in `initialize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application/finisher.rb:233:in `new'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application/finisher.rb:233:in `block in <module:Finisher>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `instance_exec'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:61:in `block in run_initializers'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `call'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:60:in `run_initializers'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:391:in `initialize!'
/home/simbed/environment/attendance_system/config/environment.rb:5:in `<main>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/zeitwerk-2.6.5/lib/zeitwerk/kernel.rb:38:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `block in require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:299:in `load_dependency'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:367:in `require_environment!'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:533:in `block in run_tasks_blocks'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:83:in `block in run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/simbed/.rbenv/versions/3.0.1/bin/rake:23:in `load'
/home/simbed/.rbenv/versions/3.0.1/bin/rake:23:in `<main>'
7000

答案1

得分: 1

我无法在 Ruby 3.1 + Rails 7.0 上重现这个问题。
另一种方法是创建一个独立的帮助对象,以避免将主对象污染可能冲突的方法:

helper = Object.new
helper.extend ActionView::Helpers::NumberHelper
puts helper.number_with_precision(3.1400, strip_insignificant_zeros: true)
英文:

I cant reproduce the issue on Ruby 3.1 + Rails 7.0.
An alternative approach is to create a standalone helper object to avoid polluting the main object with potentially conflicting methods:

helper = Object.new
helper.extend ActionView::Helpers::NumberHelper
puts helper.number_with_precision(3.1400, strip_insignificant_zeros: true)

huangapple
  • 本文由 发表于 2023年5月11日 13:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定