如何构建任务“db:populate" [英] How to build task 'db:populate'

查看:37
本文介绍了如何构建任务“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 任务

I currently have this rake task in lib/tasks/sample_data.rb

运行 rake db:populate 时出现错误,不知道如何构建任务 'db:populate'.我该如何解决这个问题?

When running rake db:populate I get the error, Don't know how to build task 'db:populate'. How do I get around this?

注意事项:我是 Rails/Ruby 的新手.我正在使用 Rails 3.

Notes: I am a newbie in Rails/Ruby. I am using Rails 3.

推荐答案

尝试将文件重命名为 sample_data.rake.

Try renaming the file to sample_data.rake.

通过将您的代码放在 lib/tasks 中名为 testomatic.rake 的文件中,我能够让您的示例工作(用 p 语句替换任务的内部结构).

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.

这篇关于如何构建任务“db:populate"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆