如何构建任务“db:populate”
1 namespace :db do
2 desc "Fill database with sample videos"
3 task :populate => :environment do
4 require 'faker'
5 Rake::Task['db:reset'].invoke
6 100.times do |n|
7 headline = Faker::Lorem.sentence(3)
8 video = Faker::Lorem.words(5)
9 Video.create!(:headline => headline,
10 :video => video)
11 end
12 end
13 end
我目前在 lib/tasks/sample_data.rb 中有这个 rake 任务,
当运行 rake db:populate
时,我收到错误,不知道如何构建任务 'db:populate'.我该如何解决这个问题?
笔记: 我是 Rails/Ruby 的新手。我正在使用 Rails 3。
1 namespace :db do
2 desc "Fill database with sample videos"
3 task :populate => :environment do
4 require 'faker'
5 Rake::Task['db:reset'].invoke
6 100.times do |n|
7 headline = Faker::Lorem.sentence(3)
8 video = Faker::Lorem.words(5)
9 Video.create!(:headline => headline,
10 :video => video)
11 end
12 end
13 end
I currently have this rake task in lib/tasks/sample_data.rb
When running rake db:populate
I get the error, Don't know how to build task 'db:populate'
. How do I get around this?
Notes:
I am a newbie in Rails/Ruby. I am using Rails 3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将文件重命名为sample_data.rake。
通过将代码放入 lib/tasks 中名为 testomatic.rake 的文件中,我能够使您的示例正常工作(用 ap 语句替换任务的内部结构)。
Try renaming the file to sample_data.rake.
I was able to get your example working (replacing the internals of the task with a p statement) by putting your code in a file called testomatic.rake in lib/tasks.
我也遇到了这个问题。在Finder中,文件名是sample_data.rake,但是在单击该文件的“获取信息”后,我发现完整的文件名是sample_data.rake.erb——确保这不是您的问题。
I also had this problem. In Finder, the file name was sample_data.rake, but upon clicking "Get Info" for the file, I discovered the full file name was sample_data.rake.erb -- ensure that's not your problem.
新手也有同样的问题 - 我的分支的命名与我合并分支时的想法不同,因此它在我创建
lib/tasks/sample_data.rake
文件之前恢复为旧文件。只需返回并使用正确的名称重新运行 git merge 并推送到 Heroku 即可正常工作。
Noobie with the same problem here - my branch was named differently than what I thought when I merged branches, so it reverted back to the old files before I had created my
lib/tasks/sample_data.rake
file.Simply going back and re-running
git merge
with the correct name andpush
ing to Heroku got it to work.