如何构建任务'db:populate'

Tee*_*eej 10 rake ruby-on-rails rake-task ruby-on-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
Run Code Online (Sandbox Code Playgroud)

我目前在lib/tasks/sample_data.rb中有这个rake任务

跑步时rake db:populate我得到错误,Don't know how to build task 'db:populate'.我该如何解决这个问题?

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

Mik*_* A. 24

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

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

  • 我自己做了同样的事情!这总是让我感到简单的事情. (2认同)