Data importer not recognizing table
I have a DataImporter class to import data from an old database, tweak it
a bit and then inject it into a new database. See my code below, it nicely
retrieves all old visits with line Visit.all from the database, but when
it wants to retrieve all old clubs with line Club.all it returns the
error: uninitialized constant DataImporter::Club
I know for sure that the Club model exists, so what is going wrong here?
class DataImporter
require 'active_record'
def import
old_clubs = get_old_clubs
old_leagues = get_old_leagues
old_visits = get_old_visits
old_visits.each do |old_visit|
old_home_club = old_clubs.where(id: old_visit.club_id).first.name
old_away_club = old_clubs.where(id: old_visit.club_away).first.name
old_league = old_leagues.where(id: old_visit.league_visited).first.name
create_new_visit(old_visit, old_home_club, old_away_club, old_league)
end
end
private
def set_connection(source)
if source == "old"
ActiveRecord::Base.establish_connection(
adapter: "xxx",
host: "xxx",
username: "xxx",
port: "xxx",
password: "xxx",
database: "xxx"
)
else
ActiveRecord::Base.establish_connection(
adapter: "xxx",
database: "xxx",
pool: "xxx",
timeout: "xxx"
)
end
end
def get_old_visits
set_connection("old")
Visit.all
end
def get_old_clubs
set_connection("old")
Club.all
end
def get_old_leagues
set_connection("old")
League.all
end
def create_new_visit(old_visit, old_home_club, old_away_club, old_league)
set_connection("new")
new_visit = Visit.new
new_visit.visit_nr = old_visit.match_nr
new_visit.the92_nr = old_visit.the92_nr
new_visit.visit_date = old_visit.visit_date
new_visit.ground = old_visit.ground
new_visit.result = old_visit.result
new_visit.season = old_visit.season
new_visit.kickoff = old_visit.kickoff
new_visit.gate = old_visit.gate
new_visit.ticket_price = old_visit.ticket_price
new_visit.countfor92 = old_visit.countfor92
new_visit.longitude = old_visit.longitude
new_visit.latitude = old_visit.latitude
new_visit.gmaps = old_visit.gmaps
new_visit.photo1 = old_visit.photo1
new_visit.photo2 = old_visit.photo2
new_visit.photo3 = old_visit.photo3
new_visit.photo4 = old_visit.photo4
new_visit.dropbox_programme = old_visit.dropbox_programme
new_visit.dropbox_ticket = old_visit.dropbox_ticket
new_visit.rating_match = old_visit.rating_match
new_visit.rating_ground = old_visit.rating_ground
new_visit.rating_atmosphere = old_visit.rating_atmosphere
new_visit.rating_trip = old_visit.rating_trip
new_visit.ticket = old_visit.ticket
new_visit.programme = old_visit.programme
new_visit.home_club = old_home_club
new_visit.away_club = old_away_club
new_visit.league = old_league
address_parts = old_visit.address.split(", ")
new_visit.street = address_parts[0]
new_visit.city = address_parts[1]
new_visit.country = address_parts[2]
new_visit.save
puts "MATCH ADDED: #{new_visit.home_club} v #{new_visit.away_club}"
end
end
No comments:
Post a Comment