3

我正在寻找编写某种 Rails 应用程序来帮助用户在餐厅预订时段。如何以这种方式对其进行建模,以便可以通过浏览器显示和预订预订?我有两个模型:

class Restaurant < ActiveRecord::Base
    has_many :reservations


class Reservation < ActiveRecord::Base
    belongs_to :restaurant

    attr_accessible :name, :date, :time

起初,我玩弄了在 Restaurant 模型中使用散列来存储可用性,使用日期作为键。但后来我意识到 Rails 数据库必须序列化哈希,我想在深入研究之前确保没有更好的方法来解决这个问题。

我正在使用 Postgres(如果相关的话)。任何帮助将非常感激!

4

2 回答 2

0

您的基本模型结构很好。请注意,attr_accessible从 Rails 4 开始,这不是当前的最佳实践。(它已被替换为强参数

于 2013-11-15T04:00:24.263 回答
0

您使用的数据库无关紧要(即使 PG 是一个可靠的选择),但真正重要的是通用工程。

我不会给你一个复制粘贴的答案,但希望能给你一些方向。

So a Restaurant can have many Reservations throughout the day. I assume each restaurant can only hold so many people and thus have some type of "reservation limit" and Reservations cannot overlap.

Because of the constraints I imagine your 2 model method will work. You just need to figure out exactly how they must interact to work as you plan.

Restaurants should be able to keep track of open times/vacancies (or whatever important details). While Reservations will keep track of the number of people in the party, time, etc.

Your initial relationship looks to be well defined. But the other answer does correctly point out the new Rails 4 preferred method.

于 2013-11-15T04:17:44.203 回答