Evgeny Palguev
Evgeny Palguev

Reputation: 609

Don't work "db.results_as_hash = true" in Ruby

I connect to database and change db.results_as_hash to true

db = SQLite3::Database.new 'barbershop.sqlite'
db.results_as_hash = true

But it don't display hash, it is simple array. And don't work

<% @results.each do |row| %>
    <tr>
        <td><%= row['Name'] %></td> 

Working code is

<% @results.each do |row| %>
    <tr>
        <td><%= row[1] %></td>

Error is "no implicit conversion of String into Integer"

What's wrong?

Ruby 2.1.5p273, SQLite version 3.8.5, MacOS X Yosemite

Upvotes: 1

Views: 1589

Answers (1)

Nick Veys
Nick Veys

Reputation: 23949

From the repo you posted. You're not actually setting that flag. The code in your question is not the code you're running. There's no db variable, and you return on the first line, so the second never runs.

def get_db
  return SQLite3::Database.new 'barbershop.sqlite'
  db.results_as_hash = true
end

I'm guessing that only compiles because the last line isn't executing. Try this:

def get_db
  db = SQLite3::Database.new 'barbershop.sqlite'
  db.results_as_hash = true
  db
end

Upvotes: 3

Related Questions