Reputation: 21
I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. The code below gives me only the first matching row instead of all matching rows, of which there should be many. It doesnt matter what I use inside where(...)
it only ever gives me the first matching row. If I use schedule.all
it gives me the entire database but only the first matching row if I use where
.
Where am I going wrong?
.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.inspect %></p>
Upvotes: 0
Views: 278
Reputation: 681
.where(...)
queries don't actually retrieve records, they return datasets that can be used to chain more queries for example:
my_posts = DB[:posts].where(:author => 'david').where(:topic => 'ruby') # no records are retrieved
If you want to actually retrieve the records, put an all
at the end:
@todaymatches = schedule.where(:date => Date.today).all # records are retrieved
See: https://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html
Upvotes: 1
Reputation: 143
This might be clunky, but I think it will do what you want. Give it a whirl.
.rb
.
.
.
get '/' do
@todaymatches = schedule.where(:date => Date.today)
def print_today_matches
@todaymatches.each { |x| puts x.inspect }
end
erb :games
end
.erb
<h1>Games</h1>
<p><%= print_today_matches %></p>
Or, alternatively:
.rb
.
.
.
get '/' do
@todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= @todaymatches.each { |x| p x } %></p>
Upvotes: 0