Rails预约网站 - 阻止某些时段/天

时间:2015-11-18 01:23:15

标签: ruby-on-rails database-design database-schema

我被要求建立一个用户可以预约汽车试驾的网站。

日历不限于10天,所以我不能事先指定日期。汽车经销商应该能够阻止某些日子或时段。

我想出了一个看起来像这样的Testdrive表:

Testdrive
---------
- id
- user_id
- date
- timeslot
- client_title
- client_name
- client_firstname
- client_company
- client_street
- client_house_nr
- client_postal_code
- client_city
- client_email
- client_phone
- client_mobile

但是,我现在还不确定如何建模“阻止”的插槽/日期。正在考虑制作另一张像“TestdriveDate”之类的东西,但后来我会将日历限制在那张桌子上...而且我不希望经销商必须启用每一天/时间段,我也不想把那么多数据放在我的数据库中。所以,我想我应该有类似“BlockedDate”或“BlockedTimeSlot”的东西。但是,在这种情况下,我必须检查我的前端列表中的每个日期对照此表..这也感觉不对。

我想'BlockedDate'方法是最好的方法吗?寻找一些建模帮助,以便我作为开发人员和我的用户(汽车经销商)使用它。

2 个答案:

答案 0 :(得分:1)

这样做:

#app/models/slot.rb
class Slot < ActiveRecord::Base
   #columns id | day | time | created_at | updated_at
   #This will be populated with all the available "slots" -- EG day 0, time 1
   enum day:  [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday]
   enum time: [:0900, :1000, :1100, :1130, :1200, :1300, :1330, :1400]

   has_many :test_drives
   has_many :clients, through: :test_drives
end

#app/models/test_drive.rb
class TestDrive < ActiveRecord::Base
   #columns id | client_id | slot_id | created_at | updated_at
   belongs_to :client
   belongs_to :slot
end

#app/models/client.rb
class Client < ActiveRecord::Base
   #columns id | title | name | company | street | house_nr | postal_code | city | email | phone | mobile | created_at | updated_at
   has_many :test_drives
   has_many :slots, through: :test_drive

   def firstname
      name.split(" ").first
   end
end

这可能有点矫枉过正,但它应该能让你做到以下几点:

@client = Client.create name: "x", etc etc
@slot = Slot.find_by day: "saturday", time: "1400"

@client.test_drives.create slot: @slot

您可以向test_drive添加有关是否已获取特定广告位的验证。

您还可以向slot模型添加验证,以确定允许哪些date / time组合:

#app/models/slot.rb
class Slot < ActiveRecord::Base
   ... 
   validate :day_times

   private

   def day_times
      permissible_times: {monday: [:0900, :1000], tuesday: [:1200]}
      errors.add(:time, "Sorry, this time is unavailable on this day") unless permissible_times[day.to_sym].include? time
   end
end

你可以read up about enum here

目前您遇到的主要问题之一是您已在TestDrives表中填入了client_个字段。虽然这会起作用,但却是一种失礼 - 它会很快变得麻烦而且负担过重。

如上所述,您与has_many :through关联会更好......

enter image description here

答案 1 :(得分:0)

这种方法怎么样。

  • 日期
  • 时隙

测试驱动

  • BLOCK_ID
  • USER_ID
  • client_title
  • CLIENT_NAME
  • client_firstname
  • client_company
  • client_street
  • client_house_nr
  • client_postal_code
  • client_city
  • client_email
  • client_phone
  • client_mobile

Block模型可以由汽车经销商或预约创建。

例如,可以有id=1的块。如果有一个block_id=1的TestDrive,那么这是一个约会。如果找不到block_id=1的TestDrive,那么这只是一个被阻止的插槽。

阻止has_one :test_drive和TestDrive belongs_to :block。 TestDrive必须与Block相关联,但Block可以没有TestDrives(零到多关系)