无法require_relative'模型'-未初始化的常量ApplicationRecord(NameError)

时间:2020-06-02 15:32:04

标签: ruby-on-rails ruby web-scraping

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

现在,我想使用此数据构建Table的类实例,并将其推入名为@tables_new的数组中。当然,我必须使用require_relative“ table”(我的模型表),但是我总是得到未初始化的常量ApplicationRecord(NameError)的错误消息。

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

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

这也无济于事。

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

有人可以帮我吗?

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

0 个答案:

没有答案