世界上最伟大的投资就是投资自己的教育
剧场模式
https://github.com/grosser/parallel
https://github.com/jfelchner/ruby-progressbar
修改后:
# frozen_string_literal: true
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
make_user!
make_posts!
make_admin_users!
end
end
PASSWORD = "qiuzhi99pro"
def make_user!
Parallel.map(1..30, progress: "Creating users...") do
email = Faker::Internet.email
User.create!(email: email,
password: PASSWORD,
password_confirmation: PASSWORD)
end
end
def make_posts!
Parallel.map(1..30, progress: "Creating posts...") do
Post.create! title: Faker::Name.name, body: "</h1>#{Faker::Lorem.sentence(word_count: 5)}</h1>"
end
end
def make_admin_users!
Parallel.map(1..30, progress: "Creating admin_users...") do
AdminUser.create! email: Faker::Internet.email, password: PASSWORD,
admin_profile_attributes: { fullname: Faker::Name.name }
end
end
修改前:
# frozen_string_literal: true
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
make_user!
make_posts!
make_admin_users!
end
end
def make_user!
bar = ProgressBar.create
password = "qiuzhi99pro"
puts "Creating users..."
User.create!(email: "me@#{ENV['domain']}",
password: password,
password_confirmation: password)
30.times do
bar.increment
email = Faker::Internet.email
User.create!(email: email,
password: password,
password_confirmation: password)
end
end
def make_posts!
bar = ProgressBar.create
puts "Creating posts..."
30.times do
bar.increment
Post.create! title: Faker::Name.name, body: "</h1>#{Faker::Lorem.sentence(word_count: 5)}</h1>"
end
end
def make_admin_users!
bar = ProgressBar.create
puts "Creating admin_users..."
(1..30).each do |n|
bar.increment
AdminUser.create! email: "example-#{n + 1}@#{ENV['domain']}", password: "qiuzhi99pro",
admin_profile_attributes: { fullname: Faker::Name.name }
end
end
课程目录
10:29FreeRuby & Rails 进阶技巧视频教程 01 用多进程来提高代码运行速度
04:402FreeRuby & Rails 进阶技巧视频教程 02 代码重构减少代码量
0 条回复
暂无回复~~
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
微信:qiuzhi99pro
b 站:https://space.bilibili.com/31152817
知乎:https://www.zhihu.com/people/rails365
Github:https://github.com/hfpp2012
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
© 汕尾市求知科技有限公司 | 创业者社区 | Rails365 Gitlab | Qiuzhi99 Gitlab | Railstart 创业项目 | 知乎 | b 站 | 搜索
粤公网安备 44152102000088号
| 粤ICP备19038915号