2017-03-18 158 views
0

我有一個叫做sk.db的SQLite3數據庫,名爲Sked的表格顯示了一個帶有列日期的體育比賽日程表。下面的代碼只給出了第一個匹配的行,而不是所有匹配的行,其中應該有很多。不管我在where(...)裏面使用什麼,它只給我第一個匹配的行。如果我使用schedule.all它給了我整個數據庫,但只有第一個匹配的行,如果我使用where爲什麼Sequel只返回第一個匹配的行?

我哪裏錯了?

.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> 

回答

0

這可能是笨重的,但我認爲它會做你想要什麼。給它一個旋轉。

.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> 

,或者:

.RB

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

.erb

<h1>Games</h1> 
<p><%= @todaymatches.each { |x| p x } %></p> 
1

.where(...)查詢不實際檢索的記錄,他們返回一個可用於鏈多個查詢,例如數據集:

my_posts = DB[:posts].where(:author => 'david').where(:topic => 'ruby') # no records are retrieved 

如果要實際檢索的記錄,把一個all末:

@todaymatches = schedule.where(:date => Date.today).all # records are retrieved 

參見:https://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html

相關問題