我有一个Rails应用程序,其SQLite数据库有一个移位表,每个Shift都有beginning_at
列,其值类型为时间,表示班次开始的小时。价值类型时间的end_at
列表示班次结束的小时。
begins_at
上午10点和ends_at
当天下午5点30分。第二条记录为Evening Shift
begins_at
下午6点30分,ends_at
第二天凌晨2点。
我还有订单表,每个订单belongs_to
都有一个班次。
问题是我想要一个控制器动作可以索引从以下创建的所有订单:此操作触发的转移的开始,直到此转变结束。< /强>
此操作的目的实际上是在每个工作班次结束时查看销售量
我认为这一切都结束了,但我无法找到起点,请帮助我。这对我的申请非常重要。
答案 0 :(得分:0)
我没有假装唯一可行的解决方案,但试试这个:
# 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。但基本方法将保持不变。