Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

ruby on rails - How to build task '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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...