Felix
Felix

Reputation: 5619

Rails calling a task in lib/tasks folder

I have a backup.rake file in the lib/tasks folder

task looks like this:

require 'find'
namespace :backup do  
  desc "Backup the database to a file." 
  task :mysqldump => [:environment] do
  end
end

Now I want to call this from a controller like this:

system "RAILS_ENV="+Rails.env+"rake backup:mysqldump"

But I got this error:

sh: 1: backup:mysqldump: not found

What is the problem? I do exactly the same with other tasks ...

Upvotes: 1

Views: 773

Answers (3)

Adnan Devops
Adnan Devops

Reputation: 503

Try this command:

Rake::Task['task_name'].invoke(args)

Upvotes: 1

Prashant4224
Prashant4224

Reputation: 1601

In controller you can use like this:

def method
  %x[rake rake backup:mysqldump]
  redirect_to root_path
end

Upvotes: 1

Sagar.Patil
Sagar.Patil

Reputation: 991

It should be like this

system "RAILS_ENV="+Rails.env+" rake backup:mysqldump"

a space between " rake

Upvotes: 3

Related Questions