原讨论帖子见:
在1.9版本中,可以使用Array#sample:
- [:foo, :bar].sample # => :foo, or :bar :-)
在1.9.2版本中实测可用。另外,从这个数组特性,我们可以将其引申到返回数据库记录组中的随机值:
- projects = Project.find(:all, :include => :users)
-
project_roles = ProjectRole.find(:all)
-
-
['Wikus van de Merwe', 'Grey Bradnam', 'Christopher Johnson', 'Piet Smit', 'Fundiswa Mhlanga', 'Tania van de Merwe', 'Obesandjo', 'Dirk Michaels', 'Ross Pienaar', 'Koobus Venter', 'Dirk Michaels', 'Sarah Livingstone'].each do |user|
- user = User.create!(:name => user)
-
- (rand(3) + 1).times do
- UserProjectParticipation.create(:user => user,:project => projects.sample,:project_role => project_roles.sample)
- end
-
end
阅读(2891) | 评论(0) | 转发(0) |