【发布时间】:2019-09-24 21:53:21
【问题描述】:
一个店铺has_many产品和一个产品belongs_to一个店铺。
该商店有opening_hours 和closing_hours 列。每列存储一个哈希,如下所示:
opening_hours = {'Monday' => '12:00', 'Tuesday' => '13:00'}
等等
我正在使用当前日期和时间,我想检索所有商店的opening_hours 小于当前时间(小于closing_hours)的产品。
基本上是开店时能买到的所有产品。
我在控制器中做了类似的事情:
day_today = Date.today.strftime('%A')
time_now = Time.new.strftime('%H:%M')
@products = Product.joins(shop: [{opening_hours[day_today] < time_now,
{closing_hours[day_today] > time_now }])
编辑
这是在数据库中存储开放时间的代码
hours_table = shop.search('table')
opening_hours = {}
closing_hours = {}
hours_table.first.search('tr').each do |tr|
cells = tr.search('td')
day = cells.first.text.strip
hours = cells.last.text.strip.tr('-', '').split
opening_hours[day] = hours[0].to_time.strftime('%H:%M')
closing_hours[day] = hours[1].to_time.strftime('%H:%M')
end
shop = Shop.new(
name: shop_name,
opening_hours: opening_hours,
closing_hours: closing_hours
)
shop.save
new_product = Product.new(
name: foo,
description: foo,
price: foo,
company: 'foo',
)
new_product.shop = shop
new_product.save
这是为 Shop 添加营业时间的迁移。关闭时间也是如此。
class AddOpeningHoursToShop < ActiveRecord::Migration[5.1]
def change
add_column :shops, :opening_hours, :json, default: {}
end
end
这是将商店 ID 添加到产品的迁移
class AddShopToProducts < ActiveRecord::Migration[5.1]
def change
add_reference :products, :shop, foreign_key: true
end
end
你猜到了吗?
【问题讨论】:
-
开盘和关盘时间是如何实际存储在数据库中的?作为每天的时间戳?作为字符串?周末或节假日呢?你会考虑到它们吗?
-
在我上面添加的代码中,我将它们转换为时间,但它们在终端中显示为字符串:{"Monday" => "12:00", "Tuesday" => "13:00" }
-
你有这些记录的例子吗? Product 和 Shop 的迁移是如何定义的?
-
刚刚添加了更多细节
标签: ruby-on-rails ruby postgresql activerecord