未定义的方法“空?” 对于#<PG::Result:0x007fcc82900a78>

Rag*_*rok 1 ruby sqlite postgresql activerecord ruby-on-rails

我们将数据库从 sqlite3 迁移到 postgreSQL。现在,在以下行中,我们收到一个错误:

  def self.get_table_id(id)
    sql = "SELECT id FROM configtables WHERE parent = 'check' AND parentid = " + id.to_s
    results = connection.execute(sql)
    return nil if results.empty? # here's where the error happens
    return results[0][0]
  end
Run Code Online (Sandbox Code Playgroud)

我对 Ruby 和 ActiveRecords 以及 postgreSQL 的了解较少。值是resultsapostgres-object还是 whats#<PG::Result:0x007fcc82900a78>以及它与sqlite3数据库的关系?

此函数是少数带有原始 sql 字符串的函数之一。

And*_*eko 5

empty?未定义为PG::Result

PG::Result.instance_methods.include?(:empty?)
#=> false
Run Code Online (Sandbox Code Playgroud)

要使用,empty?您应该将结果转换为 Array 的实例:

results.to_a.empty?
Run Code Online (Sandbox Code Playgroud)