[英]Cannot require_relative 'model' - uninitialized constant ApplicationRecord (NameError)
So I scraped data from a website into my scraper.rb
file, everything is working and fine.因此,我将网站上的数据抓取到我的scraper.rb
文件中,一切正常。
Now I want to build class instances of Table with this data, which I pushed into an array called @tables_new
.现在我想用这些数据构建 Table 的 class 实例,我将其推入一个名为@tables_new
的数组中。 Of course I have to require_relative "table" (my model table) but I always get this error message uninitialized constant ApplicationRecord (NameError).当然我必须 require_relative “表”(我的 model 表),但我总是收到此错误消息 uninitialized constant ApplicationRecord (NameError)。
So I googled and of course and already had implemented:所以我用谷歌搜索,当然已经实现了:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
which is also not helping.这也无济于事。
On the other hand could I somehow pass the @tables_new data somewhere else and create Table instances there?另一方面,我能否以某种方式将 @tables_new 数据传递到其他地方并在那里创建 Table 实例?
Anyone can help me out with this?任何人都可以帮我解决这个问题吗?
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
I encountered this issue as well when I was moving from sidekiq and redis to shoryuken and sqs.当我从 sidekiq 和 redis 转移到 shoryuken 和 sqs 时,我也遇到了这个问题。 Like @D. SM
像@D. SM
@D. SM
mentioned in the comment (thanks for the tip), I needed to load the rails environment explicitly. @D. SM
在评论中提到(感谢提示),我需要明确加载 rails 环境。 Though I've been working with rails for a while, how to manually load the rails environment in the worker was not immediately clear to me so I'm adding this answer here in case someone else encounters it.虽然我一直在使用 rails 一段时间,但我并不清楚如何在 worker 中手动加载 rails 环境,所以我在这里添加这个答案以防其他人遇到它。 I've added the following two lines at the top of my worker, adjusting the path to point to my environment.rb
:我在工人的顶部添加了以下两行,调整路径以指向我的environment.rb
:
#!/usr/bin/env ruby
require File.expand_path('../../../config/environment', __FILE__)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.