我有一个DataImporter类来从旧数据库导入数据,调整一下然后将其注入新数据库。请参阅下面的代码,它可以很好地从数据库中检索所有旧访问行Visit.all
,但当它想要检索所有带有行Club.all
的旧分会时,它会返回错误:uninitialized constant DataImporter::Club
我确信Club
模型存在,所以这里出了什么问题?
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
答案 0 :(得分:0)
确保需要使用此代码段
require 'pp'
pp $LOADED_FEATURES
如果是这样,也可以尝试使用root命名空间,方法是使用:
调用Club
类
::Club.all
祝你好运