无法require_relative 'model' - 未初始化的常量应用程序记录(名称错误)



所以我将数据从网站抓取到我的scraper.rb文件中,一切都正常。

现在我想用这些数据构建 Table 的类实例,我将其推送到一个名为@tables_new的数组中。当然,我必须require_relative"表"(我的模型表(,但我总是收到此错误消息未初始化的常量应用程序记录(NameError(。

所以我用谷歌搜索了一下,当然并且已经实现了:

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

这也无济于事。

另一方面,我可以以某种方式将@tables_new数据传递到其他地方并在那里创建表实例吗?

有人可以帮助我解决这个问题吗?

code: scraper.rb
require 'nokogiri'
require 'open-uri'
require 'mechanize'
class PingPongScraper
def initialize
@tables_new = []
@agent = Mechanize.new
end
def fetch_pingpong_tables
num_pages_to_scrape = 1
# num_pages_to_scrape = 290 // all pages to scrap from
count = 0
webcount = 0
while(num_pages_to_scrape > count)
page = @agent.get("website{webcount}").parser # took the original website out here :) 
page.css('.list_item .inner').each do |link|
@tables_new << {location: link.content.strip.split("n")[2].strip, description: link.content.strip.split("n")[1].strip}
end
webcount += 30
count += 1
end
@tables_new.each do |table|
p table
Table.create!(table)
end
end
end
r = PingPongScraper.new
r.fetch_pingpong_tables

当我从sidekiq和redis移动到shoryuken和sqs时,我也遇到了这个问题。就像评论中提到的@D. SM(感谢您的提示(,我需要显式加载轨道环境。虽然我已经使用 rails 一段时间了,但我并不清楚如何在 worker 中手动加载 rails 环境,所以我在这里添加这个答案,以防其他人遇到它。我在工作人员的顶部添加了以下两行,调整路径以指向我的environment.rb

#!/usr/bin/env ruby
require File.expand_path('../../../config/environment',  __FILE__)

最新更新