I'm getting a couple of errors in my Rails 4 appointment scheduling application that I can't seem to correct or figure out the root cause.
My seeds file keeps breaking with well known "error, stack level too deep". But when I run the method I believe it is breaking on, I get this different error:
Seeding time slots for workday no. 1 (0.5ms) SAVEPOINT active_record_1 (0.5ms) ROLLBACK TO SAVEPOINT active_record_1 fatal: exception reentered from /Users/rskelley/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.5/lib/active_record/transactions.rb:286:in `rescue in rollback_active_record_state!'
The involved files are as follows.
Seeds.rb
puts "Seeding Workdays."
day_numbers = (1..5).to_a
5.times do
start_time = WorkDay.time_slot_format(9)
end_time = WorkDay.time_slot_format([5,6][rand(2)])
rand_date = Date.today + day_numbers.slice!(0)
WorkDay.create(start_time: start_time, end_time: end_time, date: rand_date )
end
puts "Generating Time Slots for each WorkDay"
workdays = WorkDay.all
workday_number = 1
workdays.each do |workday|
calendar_manager = CalendarManager.new(workday: workday, date: workday.date)
puts "Seeding time slots for workday no. #{workday_number}"
workday_number += 1
calendar_manager.generate_time_slots!
end
calendar_manager.rb
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
extend AppointmentTools
attr_accessor :workday, :date, :slot_length
def generate_time_slots!(increment=15)
# Space between start and end
@slot_length ||= increment
day = self.workday.date
hour = 9
minute = 0
@time_slots = Array.new
33.times do
beginning = TimeOfDay.parse(hour) + minute.minutes
ending = beginning + @slot_length.minutes
time_slot = TimeSlot.create work_day_id: self.workday.id, start_time: beginning.strftime("%I:%M %p"), end_time: ending.strftime("%I:%M %p"), date: day
@time_slots << time_slot
minute += @slot_length
end
end
Going through my commit history, I am not aware of any changes made to the generate_time_slots! method, and it worked previously. I'm using Rails 4, Ruby 2.
So I just had a similar issue with a similar error message.
It is just that ActiveRecord is being "helpful" and telling you that you called the same method recursively. Basically rather than saying you ran out of stack it is looking at the trace it originally got and determining it is in fact a reentered recursive call.
At least, that solved my issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With