# Populate a table with 100,000 values in random order via migration.. QUE?!

**URL:** <https://www.ruby-forum.com/t/populate-a-table-with-100-000-values-in-random-order-via-migration-que/125428>\
**Category:** Rails\
**Created:** [January 9, 2008, 12:07am UTC](https://www.ruby-forum.com/t/populate-a-table-with-100-000-values-in-random-order-via-migration-que/125428 "2008-01-09T00:07:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MESSYKILL5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@MESSYKILL5000](https://www.ruby-forum.com/u/MESSYKILL5000)\
**Post date:** [January 9, 2008, 12:07am UTC](https://www.ruby-forum.com/t/populate-a-table-with-100-000-values-in-random-order-via-migration-que/125428/1 "2008-01-09T00:07:39Z")

</div>

HOLA! I’ve got the following ruby:

arr = (“0”…“99999”).sort{ rand(3) -1 } # shuffle-hack  
arr.each do | item |  
while item.length \< 5  
item.insert(0, ‘0’)

# puts item # Wanna see 100,000 numbers with leading zeros?

```
end

```

end

# == EOF ==

I want to put all these numbers in a table, each one a new row. Is it  
possible to do this in a migration? I’ve been trying but I get a…

> rake db:migrate…  
> Uninitialized constant Fillkeys::Key

error. (I’m putting the code in a migration, and replacing the #puts  
line with

Key.create ( :key =\> item )

Comments? Suggestions?

---

<div class="post-metadata">

**Author:** ![MESSYKILL5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@MESSYKILL5000](https://www.ruby-forum.com/u/MESSYKILL5000)\
**Post date:** [January 9, 2008, 12:25am UTC](https://www.ruby-forum.com/t/populate-a-table-with-100-000-values-in-random-order-via-migration-que/125428/2 "2008-01-09T00:25:46Z")

</div>

On Jan 8, 2008, at 6:05 PM, MESSYKILL5000 wrote:

> HOLA! I’ve got the following ruby:
> 
> arr = (“0”…“99999”).sort{ rand(3) -1 } # shuffle-hack

class Fillkeys \< ActiveRecord::Migration  
class Key \< ActiveRecord::Base; end  
def self.up  
arr = (‘00000’…‘99999’).sort\_by { rand }  
arr.each do |item|  
Key.create!(:key =\> item)  
end  
end  
end

> possible to do this in a migration? I’ve been trying but I get a…
> 
> > rake db:migrate…  
> > Uninitialized constant Fillkeys::Key

You do have a Key model in app/models/key.rb, right?

Better is to have a minimal Key model defined right inside the  
migration class itself.

> error. (I’m putting the code in a migration, and replacing the #puts  
> line with
> 
> Key.create ( :key =\> item )
> 
> Comments? Suggestions?

I hope that helps.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MESSYKILL5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@MESSYKILL5000](https://www.ruby-forum.com/u/MESSYKILL5000)\
**Post date:** [January 9, 2008, 12:43am UTC](https://www.ruby-forum.com/t/populate-a-table-with-100-000-values-in-random-order-via-migration-que/125428/3 "2008-01-09T00:43:19Z")

</div>

Yeah, Rob == the man.
