我想创建一个简单的rake函数,它为我提供了绘制随机用户的选项。
task :pick_winner => :environment do
user = User.find(:first, :order => 'RAND()')
puts "Winner #{user.name}"
end
当我运行上面的代码时,我得到以下错误
ActiveRecord::RecordNotFound: Couldn't find all Users with 'id': (first, {:order=>"RAND()"}) (found 0 results, but was looking for 2)
出了什么问题?
答案 0 :(得分:0)
这看起来像旧的Rails。
尝试:
User.order('RAND()').first