1

I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. I am simply trying to display today's matches. It appears as though the connection to the database is not working, though I do not get any errors.

I have tried looking through the Sequel documentation to no avail. How can I display results from an existing database in Sinatra?

.rb

require 'date'
require 'sequel'
require 'sinatra'

DB = Sequel.connect("sqlite://sk.db")

class Sked < Sequel::Model
end

schedule = DB.from(:sked)

get '/' do
  todaymatches = schedule.where(:date => Date.today)
  erb :games
end

.erb

 <h1>Games</h1>
 <p><%= @todaymatches %></p>
hidekinogo
  • 21
  • 3
  • Try `@todaymatches = schedule.where(...)` – Sergio Tulentsev Mar 17 '17 at 23:05
  • Thanks, I tried that but I get: #<#:0x007f929439df68> in place of the data. – hidekinogo Mar 17 '17 at 23:13
  • yeah, that's normal. That's what that object looks like as a string. If you expected to see team names and whatnot, then print them explicitly. – Sergio Tulentsev Mar 17 '17 at 23:25
  • Thanks. How do I display all the matching rows? Right now I only get one displayed. – hidekinogo Mar 17 '17 at 23:43
  • Loop over them with `.each`, for example – Sergio Tulentsev Mar 17 '17 at 23:45
  • I've tried this but evidently I'm going wrong somewhere: `

    <%= @todaygames.each{|row| p row} %>

    `
    – hidekinogo Mar 17 '17 at 23:50
  • yeah, should be more like this: http://stackoverflow.com/questions/4665279/why-is-this-rails-view-spitting-out-a-raw-array-at-the-end-of-an-each-do-loop?rq=1 – Sergio Tulentsev Mar 17 '17 at 23:54
  • I tried a couple variations on this but it doesn't seem to do the trick. `<% @todaysgames.each do |t| %> <%= t %> <% end %>` I get No Method Error. Appreciate the help. – hidekinogo Mar 18 '17 at 00:09
  • I got the erb code to work by changing the rb code to `schedule.all`. For some reason `@todaymatches` only contains one row when it should contain 5 or 6. Using `schedule.where` shouldn't `@todaymatches` contain all matching rows? – hidekinogo Mar 18 '17 at 00:17
  • After including `@todaymatches.inspect` in the erb code, it appears to only get the first row, no matter the query I place inside `where(..)`... Does anyone know why? – hidekinogo Mar 18 '17 at 00:49

1 Answers1

1

.where doesn't actually retrieve data, but instead returns a dataset. Add an .all to actually retrieve the data

todaymatches = schedule.where(:date => Date.today).all
tpei
  • 671
  • 9
  • 26