多日期选择器的 Rails 数据设计
我有一个约会模型,其 available_dates 可以包含多个日期(例如,1 月 1 日、5 日、6 日和 7 日可用)。
我的问题非常基本:我应该如何存储每个活动的可用日期?
我能想到的是一个包含两列的 Avaiable_Dates 表:event_id 和日期。每个事件都有多行,每行一个日期。查询整个表以确保我们获得事件的所有日期似乎很麻烦。哈希{事件=> {date1, date2, date3}} 会更快,但我不知道如何使用 ActiveRecord 实现它。
谢谢。
I have an Appointment model, whose available_dates can include multiple dates (say, available in Jan 1, 5, 6 and 7).
My question is very basic: how should I store available dates for each event?
What I can think of is a table Avaiable_Dates with two columns: event_id and date. Each event would have multiple rows, one date per row. It seems to be cumbersome to query entire table to make sure we got all dates of an event. A Hash {event => {date1, date2, date3}} would be faster, but I don't know how to implement it using ActiveRecord.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于可用时间使用单独的模型可能不是一个坏主意,但如果您决定采用哈希路由,则可以使用序列化关键字来执行此操作。您必须告诉 ActiveRecord 序列化变量,然后每当您访问哈希时它都会自动执行序列化和反序列化。
来自 rubyonrails.org
从设计角度来看,如果您认为您会添加如果可用时间对象有更多数据,那么您应该将其设为自己的模型。否则,序列化哈希似乎没问题。
It might not be a bad idea to just use the separate model for available times, but if you decide to go the hash route you can do so using the serialize keyword. You have to tell ActiveRecord to serialize the variable, and then it will do the serialization and deserialization automatically whenever you access the hash.
From rubyonrails.org
From a design perspective, if you think you will ever add any more data to the available time object then you should make it its own model. Otherwise a serialized hash seems fine.
我不认为你提到的单独的表有问题,我会同意。以后延长也会更容易,到时候你会感激的。
I don't see a problem with the separate table that you mentioned, I would go with that. It will also be easier to extend later which you will appreciate when the time comes.