I need to push some data from and old application using access to the new one that I created with Rails. I’ve exported Access data to .xls accessing it with the gem roo.
It works pretty fine, I created a script and by printing results I’m sure it does all properly, now I’d like to have it to insert automatically this data inside the database, accessing the rails application models.
I tried putting the file under my rails’ lib/ directory and then I required it within the environments/development.rb, but when I import the ruby file it claims that cannot find some variables I need to do the processing.
For completeness this is the whole source code I developed to extract data from the xls and edit it to match the new db schema.
# encoding: utf-8
require 'rubygems'
require 'roo'
user_id_conversion = {119 => 10, 152 => 11, 145 => 12, 161 => 13, 163 => 14, 158 => 15}
conversion_hours = {15 => 14, 17 => 16, 19 => 18}
hostess_id_conversion = {139 => 9, 157 => 17, 153 => 18, 110 => 20, 40 => 19, 141 => 21, 121 => 22, 151 => 23}
status_conversion = {1 => "Inserito", 2 => "Rifiutato", 3 => "Visitato", 4 => "Inserito", 5 => "Ricontattare", 6 => "Vendita"}
mall_id_conversion = {51 => 1, 56 => 2, 53 => 3, 54 => 4, 59 => 5, 65 => 6}
xls = Excel.new("/home/lex/Scrivania/appointments.xls")
xls.default_sheet = xls.sheets.first
city_list = []
# change this to current row numbers
1.upto(3829) do |line|
uid = xls.cell(line,'B').to_i
if user_id_conversion.has_key? uid
id = xls.cell(line, 'A')
uid = user_id_conversion[uid]
name = xls.cell(line, 'F')
surname = xls.cell(line, 'E')
city = xls.cell(line, 'O')
street = xls.cell(line, 'G')
start_at = xls.cell(line, 'C')
start_time = xls.cell(line, 'D')/60/60
street_no = xls.cell(line, 'M')
telephone = xls.cell(line, 'H')
mobile = xls.cell(line, 'I')
email = xls.cell(line, 'J')
notes = xls.cell(line, 'L')
agent_note = xls.cell(line, 'U')
status = xls.cell(line, 'S')
signed_by = xls.cell(line, 'P')
mall = xls.cell(line, 'T')
# we only want to move May -> future
if start_at.month >= Date.today.month
##
# COMPATIBILITY MODE
##
if conversion_hours.has_key? start_time
compatibility = true
else
compatibility = false
end
##
# WHO SIGNED THAT APPOINTMENT?
##
if hostess_id_conversion.has_key? signed_by
signed_by = hostess_id_conversion[signed_by]
elsif user_id_conversion.has_key? signed_by
signed_by = user_id_conversion[signed_by]
else
signed_by = -1 # display "user deleted"
end
##
# APPOINTMENT OR EXCLUDED FIELD?
##
available = true
if status == 4
available = false
end
status = status_conversion[status]
##
# todo SELECT CITY ID FROM DATABASE
##
# city = Municipality.find_by_name(city)
#
##
# CONVERT MALL_ID
##
if mall_id_conversion.has_key? mall
mall = mall_id_conversion[mall]
else
mall = nil
end
##
# EMAIL is REQUIRED
##
no_email = false
if email.blank?
no_email = true
end
##
# CONVERT APPOINTMENT DATES TO NEW ONES
##
if compatibility
old_start_at = DateTime.new start_at.year, start_at.month, start_at.day, start_time
start_at = DateTime.new start_at.year, start_at.month, start_at.day, conversion_hours[start_time]
else
old_start_at = nil
start_at = DateTime.new start_at.year, start_at.month, start_at.day, start_time
end
##
# todo interesting products
# serramenti: boolean, tende_sole: boolean, tecnic: boolean,
# tende_veranda: boolean, porte_interne: boolean, tapparelle: boolean, blindati: boolean,
# zanzariere: boolean,
##
##
# todo INSERT THIS APPOINTMENT
##
=begin
if available == false
Event.new(
:event_calendar_id => User.find(uid).event_calendar,
:status => "Inserito",
:available => false
)
else
Event.new(
:event_calendar_id => User.find(uid).event_calendar,
:signed_by_id => User.find_by_id(signed_by),
:mall_id => Mall.find_by_id(mall),
:status => status,
:name => name,
:surname => surname,
:street => street,
:street_no => street_no,
:city_id => city.name,
:telephone => telephone,
:mobile => mobile,
:email => email,
:no_email => no_email,
:inserting_notes => notes,
:seller_notes => agent_note,
:start_at => start_at,
:available => true,
group_id: 3,
:compatibility_start_at => old_start_at
)
end
=end
end
end
end
How can I make this work inside the rails environment?
I was thinking to make something like a class or a function to be run inside the rails c console because I’ll need to run this this evening to get the most updated database version, extract the xls with updated records and do the magic.
Sorry if this may seem a noob question but I’m new to Ruby.
P.S. I’m using Rails 3.2.3 w/Ruby 1.9.3
P.P.S. I inserted ‘roo’ inside the Gemfile since it was first not finding the gem.
Thanks.
If you do
Then rails will load the rails environment and then run your script