开发者

rails rake runs lib/tasks/scrape.rake when running rake db:migrate

开发者 https://www.devze.com 2023-02-16 09:00 出处:网络
I have a trivial rake script that scrapes a web page. It\'s path is lib/tasks/scrape.rake. dan@snowcrash:~/snowcrash$ ls lib/tasks/

I have a trivial rake script that scrapes a web page. It's path is lib/tasks/scrape.rake.

dan@snowcrash:~/snowcrash$ ls lib/tasks/
scrape.rake

I also have some database migration scripts in db/migrate:

dan@snowcrash:~/snowcrash$ ls db/migrate/
20110307213515_create_questions.rb
20110310010000_create_answers.rb
20110313191820_add_question_id_to_answers.rb

The problem is when I run rake db:migrate. It appears to also run my lib/tasks/scrape.rake task:

dan@snowcrash:~/snowcrash$ rake db:migrate
(in /home/dan/snowcrash)
Fetched document: http://www.baidu.开发者_高级运维com/s?wd=love+me
\t Content Type: text/html\n
\t Charset: gbk\n
\t Content-Encoding: \n
\t Last Modified: \n\n
==  AddQuestionIdToAnswers: migrating =========================================
-- add_column("questions", "answer_id", :integer)
   -> 0.4923s
-- add_index("questions", "answer_id")
   -> 0.4954s
==  AddQuestionIdToAnswers: migrated (0.9881s) ================================

I did some googling, and found this doc: http://jasonseifer.com/2010/04/06/rake-tutorial. He states:

Rails will autmatically pick up tasks in lib/tasks.

How do I have "rake db:migrate" not run my other rake tasks? What options do I have?


Without seeing the contents of your scrape.rake task, it's hard to be sure what's going on, but my guess is that the functionality of your scrape task isn't actually wrapped up in a task. For example, here's a proper rake task:

desc "Output something on the command line"
task :output_stuff do
  puts "I'm outputting stuff!"
end

If instead the contents of your .rake file were just:

puts "I'm outputting stuff!"

then when Rails loads your rake task, the code is executed immediately (Rake tasks are written in Ruby after all!)

So, check that, and let me know if I'm way off base.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号