我不会假装永远的唯一可能的解决方案,但试试这个:
# 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