简体   繁体   English

查询嵌套属性

[英]Querying on nested attribute

I have a set of stores, that again have locations . 我有一组商店,又有一个locations These locations have different business_hours . 这些位置具有不同的business_hours The way I store the business hours are: 我存储工作时间的方式是:

 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

so I have a time on open_at and at close_at . 所以我有一些时间在open_atclose_at What I'd like to get from the store, is the locations that are available at the time that I query. 我想从商店获得的是查询时可用的位置。

So for example today is Wednesday, and my local time is 12:40. 例如,今天是星期三,我的当地时间是12:40。 I want it to return all the locations that are open on Wednesday at 12:40. 我希望它返回星期三12:40开放的所有位置。

I've started writing a method but unsure how to complete it. 我已经开始编写方法,但不确定如何完成。

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
store.locations.join(:business_hours).where("week_day = ? AND open_at < ? < close_at", current_day, Time.zone.now)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM