【发布时间】:2016-04-27 17:59:40
【问题描述】:
我有一个 Day 模型,一个 day 模型可以有多个 time_slots。
日模型如下:
# == Schema Information
#
# Table name: days
#
# id :integer not null, primary key
# schedule_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# wday :integer
#
class Day < ActiveRecord::Base
belongs_to :schedule
has_many :time_slots, :dependent => :destroy
end
TimeSlot 模型如下所示:
# == Schema Information
#
# Table name: time_slots
#
# id :integer not null, primary key
# day_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# pin :string
# start_hour :integer
# start_minute :integer
# end_hour :integer
# end_minute :integer
#
class TimeSlot < ActiveRecord::Base
belongs_to :day
has_many :passes, :dependent => :destroy
validates :start_hour, :presence => true
validates :start_minute, :presence => true
validates :end_hour, :presence => true
validates :end_minute, :presence => true
end
我目前有以下工厂:
# == Schema Information
#
# Table name: days
#
# id :integer not null, primary key
# schedule_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# wday :integer
#
FactoryGirl.define do
factory :day do
wday rand(0..6)
after(:create) do |day, evaluator|
create_list(:time_slots, 2, day: day,start_hour: generate :start_hour, start_min: generate :start_min, end_hour: generate :end_hour,end_min: generate :end_min)
end
end
sequence :start_hour do |n|
#Needs to return something between 00 and 23
n
end
sequence :start_min do |n|
#Needs to return something between 00 and 55
n
end
sequence :end_hour do |n|
#Needs to return something between 00 and 23 but a value HIGHER than start_hour
#If end_min == start_min
n
end
sequence :end_min do |n|
#Needs to return something between 00 and 55 but a value that is definitely after start_min if end_hour and start_hour are equal
n
end
end
基本上 - 我的一天必须有有意义的 time_slots,即一天可能有以下有效:
Timeslot 1
start_hour : 10
start_min : 00
end_hour : 11
end_min : 00
Timeslot 2
start_hour : 11
start_min : 00
end_hour : 12
end_min : 00
但是这将是无效的,因为时隙 2 与时隙 1 重叠。同样 2 个相同的 time_slots 无效:
Timeslot 1
start_hour : 10
start_min : 00
end_hour : 11
end_min : 00
Timeslot 2
start_hour : 10
start_min : 30
end_hour : 11
end_min : 00
似乎序列在这里可以帮助我...如果我有一组“全局”的开始时间、开始时间等,这将很容易,因为我可以从数组中选择下一个但我没有不知道这是否可能或是否有更好的方法?
请帮忙!
【问题讨论】:
标签: ruby-on-rails rspec factory-bot rspec-rails