有没有办法将 property.method 传递给 .where() 而不是 Rails 中的 属性?

Is there a way to pass property.method to .where() instead of property in Rails?

我的日程表模型如下所示:

    create_table "schedules", force: :cascade do |t|
        t.integer  "week_day"
        t.time     "opening_time"
        t.time     "closing_time"
        t.datetime "created_at",    null: false
        t.datetime "updated_at",    null: false
        t.integer  "taco_place_id"
      end
add_index "schedules", ["taco_place_id"], name: "index_schedules_on_taco_place_id"

如您所见,有 opening_time 和 closing_time 属性,我有一个 realtionship Schedule belongs_to :taco_place 和 TacoPlace has_many :schedules , 依赖: :destroy.

我试图从日程表模型中获取 TacoPlace 今天的实际日程表(如果存在)。

我已经实现了 TacoPlace 今天的时间表(取决于 week_day 属性)的范围,如下所示:

scope :today_for_taco_place, ->(taco_place){where(taco_place_id: taco_place.id, week_day: Time.now.wday)}

我在这个方法中使用它:

def self.actual_for_taco_place(taco_place)
    today = self.today_for_taco_place(taco_place)
    today.where("opening_time <= :now and closing_time >= :now", now: Time.now.utc).first 
end

我已经测试过它 "works"。问题是,如果我在控制台上 运行 "Schedule.first.opening_time" 我得到“2000-01-01 06:00:00 UTC”。如您所见,它不仅包括时间,还包括日期(即使播种为 "opening_time: "15:00".to_time, closing_time: "24:00 ".to_time").

最后,问题来了:

有什么方法可以 运行 像这样:(我知道这还行不通)

def self.actual_for_taco_place(taco_place)
        today = self.today_for_taco_place(taco_place)
        today.where("#{opening_time.strftime("%H%M")} <= :now and #{closing_time.strftime("%H%M") >= :now", now: Time.now.utc.strftime("%H%M")).first 
    end

这样 .where() 方法就不会查找 属性(opening_time 或 closing_time),而是执行 strftime() 方法以便我可以进行比较只有时间?或者我应该将 opening_time 和 closing_time 保存为整数(即​​“1200”)还是在方法中手动转换它们?

抱歉,如果我的问题很长或难以理解。预先感谢您的建议。

Opening_time 和 closing_time 现在是整数。我发现我没有从 "time" 而不是 "integer" 中得到任何东西,因为它只代表一个小时。