Ruby on rails 嵌套属性查询

Ruby on rails 嵌套属性查询,ruby-on-rails,Ruby On Rails,我有一套商店,它们同样有位置。这些地点的营业时间不同。我存储营业时间的方式是: create_table "business_hours", force: :cascade do |t| t.time "open_at" t.time "close_at" t.integer "week_day", default: 0, null: false t.integer "location_id" t.datetime "creat

我有一套商店,它们同样有
位置
。这些地点的营业时间不同。我存储营业时间的方式是:

 create_table "business_hours", force: :cascade do |t|
    t.time     "open_at"
    t.time     "close_at"
    t.integer  "week_day",    default: 0, null: false
    t.integer  "location_id"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
    t.index ["location_id"], name: "index_business_hours_on_location_id", using: :btree
  end
因此,我在的
open\u和
close\u上有一段时间。我想从商店得到的是我查询时可用的位置

比如今天是星期三,我的当地时间是12:40。我希望它能返回周三12:40开放的所有地点

我已经开始写一个方法,但不确定如何完成它

def locations_open(local_time: Time.zone.now.in_time_zone('Stockholm'))
 current_day = local_time.wday
 store.locations.join(:business_hours).where(business_hours: { week_day: current_day })
 # and current time is open_at <> close_at
end
def locations\u open(本地时间:time.zone.now.in\u时区(“斯德哥尔摩”))
当前天=本地时间.wday
store.locations.join(:business_hours).where(business_hours:{周日:当前日})
#当前时间为打开时\u关闭时\u
结束
store.locations.join(:business\u hours)。where(“week\u day=”和open\u at<?
store.locations.join(:business\u hours)。where(“week\u day=”和open\u at<?
在这些情况下,您可以使用
scope
来存储打开的时间和关闭的时间。在这些情况下,您可以使用
scope
来存储打开的时间和关闭的时间。如何存储打开的时间和关闭的时间?请尝试以下操作:
store.locations.join(:business_hours)。where(“周日=”和打开的时间<和关闭的时间)、当前的时间、时区、现在、时区、现在)
据我所知,这两个版本都可以正常工作。你解决问题了吗?试试这个:
store.locations.join(:business\u hours)。where(“week\u day=”和“open\u at”和“close\u at”、current\u day、Time.zone.now、Time.zone.now)
,据我所知,这两个版本都可以正常工作。你解决问题了吗?
store.locations.join(:business_hours).where("week_day = ? AND open_at < ? < close_at", current_day, Time.zone.now)