Question

I am trying to automate a task with Heroku Scheduler. I tested the task in local and it works. I created a temporary route for testing, and it works everytime I hit the route:

get 'create_expired_credit_lines' to: 'credits#create_expired_lines'

credits.controller.rb

class CreditsController < ApplicationController
...

 def self.create_expired_lines
 ...
  @new_credit = Credit.new(params[:credit])
 ...
 end

I then created a file

app/lib/tasks/scheduler.rake

desc "This task is called by the Heroku scheduler add-on"
task :create_expired_credit_lines => :environment do
  puts "Creating expired credit lines..."
  CreditsController.create_expired_lines
  puts "done."
end

When the tasks get executed, I get the following error:

2014-03-20T21:42:11.190318+00:00 app[scheduler.4496]: undefined local variable or method `params' for CreditsController:Class

Full log:

2014-03-20T21:42:06.228208+00:00 heroku[scheduler.4496]: Starting process with command `bundle exec rake create_expired_credit_lines`
2014-03-20T21:42:08.725450+00:00 heroku[scheduler.4496]: State changed from starting to up
2014-03-20T21:42:09.410565+00:00 app[scheduler.4496]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
 2014-03-20T21:42:09.410565+00:00 app[scheduler.4496]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
2014-03-20T21:42:10.274625+00:00 app[scheduler.4496]: Connecting to database specified by DATABASE_URL
2014-03-20T21:42:10.563939+00:00 app[scheduler.4496]: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
2014-03-20T21:42:10.852664+00:00 app[scheduler.4496]: Creating expired credit lines...
2014-03-20T21:42:11.190030+00:00 app[scheduler.4496]: rake aborted!
2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each'
2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each'
2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/app/controllers/credits_controller.rb:147:in `create_expired_lines'
2014-03-20T21:42:11.191569+00:00 app[scheduler.4496]: (See full trace by running task with --trace)
2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/app/controllers/credits_controller.rb:158:in `block in create_expired_lines'
2014-03-20T21:42:11.190318+00:00 app[scheduler.4496]: undefined local variable or method `params' for CreditsController:Class
2014-03-20T21:42:11.191478+00:00 app[scheduler.4496]: Tasks: TOP => create_expired_credit_lines
2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/lib/tasks/scheduler.rake:4:in `block in <top (required)>'
2014-03-20T21:42:12.567208+00:00 heroku[scheduler.4496]: Process exited with status 1
2014-03-20T21:42:12.576344+00:00 heroku[scheduler.4496]: State changed from up to complete

===========

UPDATE: I removed the arguments from the method Credit.new(params[:credit]) and it now looks like this:

credits.controller.rb

class CreditsController < ApplicationController
...
def new
...
@credit = Credit.new
@credit.user_id = params[:id]
... 
end


 def self.create_expired_lines
 ...
  @new_credit = Credit.new(params[:credit])
 ...
 end

When I run the command

$ heroku run rake create_expired_credit_lines

I get the following errors:

Running `rake create_expired_credit_lines` attached to terminal... up, run.3757
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
Connecting to database specified by DATABASE_URL
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to     skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Creating expired credit lines...
rake aborted!
undefined local variable or method `params' for CreditsController:Class
/app/app/controllers/credits_controller.rb:177:in `block in create_expired_lines'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-  3.2.11/lib/active_record/relation/delegation.rb:6:in `each'
 /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each'
/app/app/controllers/credits_controller.rb:154:in `create_expired_lines'
/app/lib/tasks/scheduler.rake:4:in `block in <top (required)>'
Tasks: TOP => create_expired_credit_lines
(See full trace by running task with --trace)
Was it helpful?

Solution

You should define the create_expired_lines method with arguments

def self.create_expired_lines(args)
 ...
  @new_credit = Credit.new(args)
 ...
 end

As you don't have access to params hash at this level.

And you can call this method from RSpec with proper arguments.

For eg:

CreditsController.create_expired_lines(attr1: value, attr2: value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top