我安装了'faker'宝石但是当我使用它时(Faker :: Book.name)它不显示随机名称,而只显示一个字符串“Faker :: Book”
lib>任务> data_populate
require 'colorize'
namespace :data_populate do
namespace :populate do
desc "Populate all model data set"
task :all => :environment do
require 'faker'
puts "\n'Populating Model Data'".yellow
Job.where(name: "Faker::Book").destroy_all
10.times do |l|
Job.create(name: Faker::Book.name, description: Faker::Lorem.sentence(7), business_id: 1)
end
end #all end
end #populate end
end #end rake dashboard
当代码运行时,作业标题始终是“Faker :: Book”
答案 0 :(得分:0)
"Faker::Book"
不能存储到db,它的名称方法只需选择一个随机名称值,因此它不能在where
子句中按类名匹配,所以你只需要尝试定义一些constance命名部分,并匹配它:
Job.where("name LIKE 'faker%'").destroy_all
10.times do |l|
Job.create(name: "faker " + Faker::Book.name, description: Faker::Lorem.sentence(7), business_id: 1)
end