Rake is one of those tools that you don't realize how powerful it is until you start using it. In this episode you will learn how to create custom rake tasks and improve them by using rake features.
namespace :pick do
desc "Pick a random user as the winner"
task :winner => :environment do
puts "Winner: #{pick(User).name}"
end
desc "Pick a random product as the prize"
task :prize => :environment do
puts "Prize: #{pick(Product).name}"
end
desc "Pick a random prize and winner"
task :all => [:prize, :winner]
def pick(model_class)
model_class.find(:first, :order => 'RAND()')
end
end
本文介绍如何利用Rake工具创建自定义任务,通过示例展示了如何挑选随机用户作为赢家及随机奖品,并组合这两个任务实现自动化抽奖流程。
4482

被折叠的 条评论
为什么被折叠?



