Skip to content

Instantly share code, notes, and snippets.

@stansidel
Last active January 4, 2016 05:29
Show Gist options
  • Save stansidel/8575730 to your computer and use it in GitHub Desktop.
Save stansidel/8575730 to your computer and use it in GitHub Desktop.
Setting param with random records of another model
#rand(3..150).times do
# tender.items << Item.first(offset: rand(Item.count))
#end
# See http://hashrocket.com/blog/posts/rails-quick-tips-random-records
#tender.items = Item.where(Item.pluck(:id).shuffle[0..rand(3..150)])
# For Postgres
tender.items = Item.limit(rand(3..150)).order('RANDOM()')
# For MySQL
#tender.items = Item.limit(rand(3..150)).order('RAND()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment