a script for connect a database and copy a single table in two tables, copy and save with the .rb extension for execute with Ruby, for example "inicio2tablas.rb" and run in a command line
cmonterrosa@dibd02:~$ ./inicio2tablas.rb mydatabase myhost
require 'rubygems'
require 'active_record'
def usage
STDERR.puts "Uso:
#{File.basename $0} dbname host"
exit 1
end
usage if ARGV.size != 2
dbname = ARGV[0]
host = ARGV[1]
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => dbname,
:username => 'your-user',
:password => 'your-password',
:encoding => 'utf8',
:host => host )
class Inicio < ActiveRecord::Base
set_table_name :inicio
end
class Palabra < ActiveRecord::Base
set_table_name :palabras
has_many :significados
end
class Significado < ActiveRecord::Base
set_table_name :significados
belongs_to :palabra
end
puts "Cargando palabras..."
@contador=0
@inicio= Inicio.find(:all)
@inicio.each do |inicio|
@palabra = Palabra.create(:palabra => inicio.palabra,
:abreviatura => inicio.abreviatura)
@contador+=1
(1..12).each do |num|
next if inicio["significado#{num}"].nil? || inicio["significado#{num}"].empty?
@palabra.significados.create(:significado => inicio["significado#{num}"])
end
end
puts "Finalizo la carga de palabras.."
puts "Total: #{@contador}"
No hay comentarios:
Publicar un comentario