ruby-on-railsrakeactionviewhelper

Rake task with both Active Record and View Helpers


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

Solution

  • 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)