We migrated our database from sqlite3 to postgreSQL. Now, at the following line, we get an error:
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
I have less knowledge of Ruby and ActiveRecords and with postgreSQL as well. Is the value in results a postgres-object, or whats #<PG::Result:0x007fcc82900a78> and what was it with the sqlite3 database?
This function is one of a few with raw sql-strings.
empty? is not defined for PG::Result:
PG::Result.instance_methods.include?(:empty?)
#=> false
To use empty? you should convert the result to an instance of Array:
results.to_a.empty?
Seems like you should "fetch" actual results from PG::Result object.
One way of doing this could be calling to_a
Try the following:
sql = "SELECT id FROM configtables WHERE parent = 'check' AND parentid = " + id.to_s
results = ActiveRecord::Base.connection.execute(sql).to_a
...