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

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?

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
[email protected]

Yeah, Rob == the man.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs