如何执行Rails“Rake任务”

时间:2013-06-17 13:25:42

标签: ruby-on-rails ruby-on-rails-3 rake task

怎么了?

我的朋友创建了一个rake任务来更新数据库中的数据(因为我们有db更改)。 以下是任务:

namespace :db do

  task :update_database => :environment do
    puts "Update do banco"

    posts = Post.where("source_id is null").order("id")
    done = Array.new
    posts.each do |post|
      if post.source_id.nil? and !done.include?(post)
        posts2 = Post.where("content LIKE ? AND id != ?", post.content, post.id)
        done.concat(posts2)
        posts2.each do |post2|
          post2.source_id = post.id
          post2.save
        end
      end
    end

  end
end

我已经在我的localhost中执行了这个rake任务,但是我将我的项目部署到heroku,现在我的项目无法在线打开。我不记得执行rake任务的命令是什么,我无法在任何地方找到它。

我的问题是:

  1. 执行rake任务的命令是什么?
  2. 在heroku上执行rake任务的命令是什么?只是“heroku跑”?
  3. 谢谢!

1 个答案:

答案 0 :(得分:3)

heroku run bundle exec rake db:update_database

应该这样做。

bundle exec确保脚本在当前bundle的上下文中运行。