多日期选择器的 Rails 数据设计

发布于 2024-12-10 08:25:23 字数 276 浏览 2 评论 0原文

我有一个约会模型,其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-12-17 08:25:23

对于可用时间使用单独的模型可能不是一个坏主意,但如果您决定采用哈希路由,则可以使用序列化关键字来执行此操作。您必须告诉 ActiveRecord 序列化变量,然后每当您访问哈希时它都会自动执行序列化和反序列化。

在文本列中保存数组、散列和其他不可映射的对象

Active Record 可以使用 YAML 序列化文本列中的任何对象。为此,您必须通过调用类方法序列化来指定这一点。这使得无需执行任何额外工作即可存储数组、哈希和其他不可映射对象。

class User < ActiveRecord::Base
  serialize :preferences
end

user = User.create(:preferences => { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }

您还可以指定一个类选项作为第二个参数,如果将序列化对象作为不在层次结构中的类的后代进行检索,该选项将引发异常。

class User < ActiveRecord::Base
  serialize :preferences, Hash
end

user = User.create(:preferences => %w( one two three ))
User.find(user.id).preferences    # raises SerializationTypeMismatch

当您指定类选项时,该属性的默认值将是该类的新实例。

class User < ActiveRecord::Base
  serialize :preferences, OpenStruct
end

user = User.new
user.preferences.theme_color = "red"

来自 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.

Saving arrays, hashes, and other non-mappable objects in text columns

Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work.

class User < ActiveRecord::Base
  serialize :preferences
end

user = User.create(:preferences => { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }

You can also specify a class option as the second parameter that’ll raise an exception if a serialized object is retrieved as a descendant of a class not in the hierarchy.

class User < ActiveRecord::Base
  serialize :preferences, Hash
end

user = User.create(:preferences => %w( one two three ))
User.find(user.id).preferences    # raises SerializationTypeMismatch

When you specify a class option, the default value for that attribute will be a new instance of that class.

class User < ActiveRecord::Base
  serialize :preferences, OpenStruct
end

user = User.new
user.preferences.theme_color = "red"

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.

我不吻晚风 2024-12-17 08:25:23

我不认为你提到的单独的表有问题,我会同意。以后延长也会更容易,到时候你会感激的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文