1

I have a rake task and i'm trying to find the record in the database to be updated. Problem is when i use (params[:name]) it throws out the message undefined local variable or method `params' for main:Object. How do i make params available to the rake task? Thanks. I'm new to this and any help is appreciated.

task scrape_borisAPI: :environment do

@json = HTTParty.get("http://borisapi.heroku.com/stations.json")

@json.each do |station|

@location = Location.find(params[:name])
    @location.nb_bikes = station["nb_bikes"]
    @location.nb_empty_docks = station["nb_empty_docks"]
    @location.update_attributes(:nb_bikes => "@location.nb_bikes", :nb_empty_docks => "@location.nb_empty_docks")
end
end

Originally the rake task looked like this and i'm trying to modify it to update records as opposed to creating new records.

task scrape_borisAPI: :environment do

@json = HTTParty.get("http://borisapi.heroku.com/stations.json")

@json.each do |station|
        @location = Location.new
    @location.name = station["name"]
    @location.latitude = station["lat"]
    @location.longitude = station["long"]
    @location.nb_bikes = station["nb_bikes"]
    @location.nb_empty_docks = station["nb_empty_docks"]
    @location.save!
    end
end
  • You mean you want to be able to pass arguments to a rake task to use them in it? Or, you want to use the `params` from a request in Rails? – Leo Correa Mar 16 '14 at 15:09
  • i essentially want the name of each record in the database to be available for the rake task? Does that make sense? thanks –  Mar 16 '14 at 15:12
  • I understand a bit what you want to do but not how you want to accomplish it. What exactly are you expecting as `params[:name]`. `params` is just a method that is in the context of a request in Rails. You won't find `params` in a Rake task. If what you want is to pass in an argument to a Rake task that's a different thing. Do you understand what a Rake task is? – Leo Correa Mar 16 '14 at 15:22
  • I think i do, but now i may doubt myself!I've edited the original question, maybe that helps? I'm trying to find the existing record in the database and then update it.I don't know whether passing an argument in is what i want, i just need the rake to be able to look through the database and if it finds a record with a matching name, update it –  Mar 16 '14 at 15:34

2 Answers2

0

You can't. Rake tasks are not Web Requests, only web requests have params. How to do it? Use ENV!

task scrape_borisAPI: :environment do

unless ENV['name']
  puts "You should set ENV 'name' to something to perform this task!"
  next # Use next, see this Stackoverflow question: http://stackoverflow.com/questions/2316475/how-do-i-return-early-from-a-rake-task
end

@json = HTTParty.get("http://borisapi.heroku.com/stations.json")

@json.each do |station|

@location = Location.find(ENV['name'])
    @location.nb_bikes = station["nb_bikes"]
    @location.nb_empty_docks = station["nb_empty_docks"]
    @location.update_attributes(:nb_bikes => "@location.nb_bikes", :nb_empty_docks => "@location.nb_empty_docks")
end
end

Then you can run your task in this way:

bundle exec rake scrape_borisAPI name=Something
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • Thanks for your help but i just don't know how to do what you suggest. –  Mar 16 '14 at 16:27
  • What you mean? I wrote the entire code even the bash command, you don't need anything else – Francesco Belladonna Mar 16 '14 at 16:51
  • I mean that it doesn't do anything...i was trying to edit it because it wasn't working. Sorry. –  Mar 16 '14 at 16:58
  • That is your code, you should make it works, I just copy-pasted what you wrote in your question. I only added ENV['name'] thing which **is working**, I have it working in thousands tasks I wrote so is nothing special (it's also very simple code, hard to create a bug). If your API scraping code is not working, you should check it by yourself, but that's not the point of this question. – Francesco Belladonna Mar 16 '14 at 17:03
0

Since you will be calling rake task from within the code you can write a rake task that takes in arguments.

namespace :whatever do desc "desc of task" task :task_name, [:arg1] => :environment do |task, args| puts "Your argument is #{arg.arg1}" #Do all that you want. end end

you can write your description of rake task and then call it with that param that you want and you can to the update operations. Calling the task is like : rake whatever:taskname[argument]

AshwinKumarS
  • 1,303
  • 10
  • 13