0

I'm trying to add a rake task from a gem (geocoder https://github.com/alexreisner/geocoder) from a rake task of my application, as i would like it to be run just after the database is built.

So i have this code inside a rakefile

task :geolocal do
    spec = Gem::Specification.find_by_name 'geocoder'
    load "#{spec.gem_dir}/lib/tasks/geocoder.rake"
    puts '##################GEOCODING##############'
    Rake::Task["geocode:all CLASS=ProposedAccomodation"].execute
end

just following this question Ruby Rake load tasks from a gem but i keep getting the same error

Don't know how to build task 'geocode:all CLASS=ProposedAccomodation'

any clue what i'm doing wrong?

Community
  • 1
  • 1
sissy
  • 2,908
  • 2
  • 28
  • 54
  • possible duplicate of [Rake Execute With Multiple Arguments](http://stackoverflow.com/questions/8033303/rake-execute-with-multiple-arguments) – phoet Oct 10 '14 at 12:43
  • you have to pass the class as an argument to execute. – phoet Oct 10 '14 at 12:43
  • @phoet i'm trying since one hour to specify the argument, in different ways, seen in the question you linked, as well as these ones http://stackoverflow.com/questions/825748/how-do-i-pass-command-line-arguments-to-a-rake-task, but i keep getting a 'Please specify a CLASS (model)' error. stuff_args = {"CLASS" => "ProposedTodo" } This is what i'm trying last: 'Rake::Task["geocode:all"].execute(Rake::TaskArguments.new(stuff_args.keys, stuff_args.values))' any clue? – sissy Oct 10 '14 at 14:36
  • could it possibly be the uppercase name of the argument? – sissy Oct 10 '14 at 14:46

1 Answers1

0

so there are several things to be taken into consideration here.

for the record, this is how you call a task with arguments

Rake::Task["taskname"].execute(args)

in your case, i did not realize first, that it actually uses environment variables instead of task arguments that are read as ENV['CLASS'].

that would answer your question, so you can either set it ENV['CLASS'] = 'ClassName' or pass it along to your call to the rake task rake geolocal CLASS=ProposedAccomodation.

which brings me to a following question: why are you not just calling the original rake task, there is nothing you add to it.

phoet
  • 18,688
  • 4
  • 46
  • 74
  • I see. That is actually working, to set the argument inside the task like ENV['CLASS'] = 'Class' and is what i was looking for. The advantage of doing it inside a single rake task is that i have several calls to the same task that i wanted to group together and this way it works. Thx – sissy Oct 13 '14 at 09:22