我不會假裝永遠的唯一可能的解決方案,但試試這個:
# orders.rb
class Order < ActiveRecord::Base
belongs_to :shift
scope :at_morning_shift, -> { where(shift: Shift.morning).order('created_at asc') }
scope :at_night_shift, -> { where(shift: Shift.night).order('created_at asc') }
def self.current_shift
begins_time, ends_time = Shift.morning.begins_at, Shift.morning.ends_at
if (begins_time.hour..ends_time.hour).cover?(Time.now.hour)
Order.at_morning_shift
else
Order.at_night_shift
end
end
end
# shift.rb
class Shift < ActiveRecord::Base
has_many :orders
def self.morning
find_by_name("Morning shift")
end
def self.night
find_by_name("Night shift")
end
end
# orders_controller.rb
class OrdersController < ApplicationController
def index
@orders = Order.current_shift
end
end
這裏是控制檯輸出:
[86] pry(main)> Time.now.utc
=> 2016-08-22 05:05:50 UTC
[87] pry(main)> Order.current_shift
Shift Load (0.3ms) SELECT "shifts".* FROM "shifts" WHERE "shifts"."name" = $1 LIMIT 1 [["name", "Morning shift"]]
Shift Load (0.2ms) SELECT "shifts".* FROM "shifts" WHERE "shifts"."name" = $1 LIMIT 1 [["name", "Morning shift"]]
Shift Load (0.2ms) SELECT "shifts".* FROM "shifts" WHERE "shifts"."name" = $1 LIMIT 1 [["name", "Night shift"]]
Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."shift_id" = 2 ORDER BY created_at asc
=> [#<Order:0x007fbdc88888c0 id: 1, name: "molestiae", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:32:53 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:32:53 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc8888730 id: 5, name: "explicabo", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc8888578 id: 6, name: "cupiditate", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc88883e8 id: 7, name: "rerum", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc8888258 id: 8, name: "totam", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc88880c8 id: 9, name: "inventore", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>,
#<Order:0x007fbdc8883ed8 id: 11, name: "saepe", phone: nil, product_id: nil, created_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, updated_at: Mon, 22 Aug 2016 04:33:40 UTC +00:00, shift_id: 2>]
[88] pry(main)>
請記住,你要注意到您的應用程序和客戶端的time_zone。但基本方法將保持不變。
非常感謝您的回答。它工作得很好,除了檢索當前班上創建的所有記錄外,即使它是在一週前創建的。我只希望只回收過去的訂單。 – Opapadaia
每天創造的班次? EG今天你會有兩班倒,明天你會再有兩班倒?所以你的數據庫每天會有兩個新班次? – retgoat
不,我的意思是將結果限制爲僅在過去的早上或晚上創建的訂單。我試圖調整查詢,我想出了......'Order.where(「created_at> =?AND shift_id =?」,Time.current.in_time_zone(「Zone」)。beginning_of_day,Shift.morning',It在控制檯中工作得很好,但是當我把它放在範圍內時,模型完全忽略了創建日期! – Opapadaia