Rails factory girl 这套关联模型如何创建工厂

Rails factory girl how can I create a factory for this set of associated models

我有一个日模型,一个日模型可以有很多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

似乎序列可以在这里帮助我...如果我有一组 'global' 开始分钟、开始时间等,这会很容易,因为我可以从数组中选择下一个但我不知道这是否可行,或者是否有更好的方法?

请帮忙!

实际上,我想我不会为此使用 factory_girl。相反,我会 明显地 在测试中创建我的时间段。

但是,你可以这样做:

sequence :start_hour do |n|
  #Needs to return something between 00 and 23
  raise 'No more timeslots available' if n == 24
  n - 1 # if factory_girl starts with 1
end

sequence :start_min do |n|
  #Needs to return something between 00 and 55
  0 # Just always zero
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 - 1
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 
  55 # like always 55 minute slots
end

基本上,您可以使用 n

希望factory_girl与n一致。否则,您可以使用 n.

尝试更大的循环