简体   繁体   中英

Why does Sequel return only the first matching row?

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>

.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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM