I'm working in a project with the following structure.
A location can have many timetables with many lessons.
I need to perform a job in all locations every sunday to create or update the lessons.
def perform(*args) locations = Location.all locations.each do |location| timetables = LocationLessonService.fetch(location.id)["timetables"] timetables.each do |timetable| lessons = timetable["lessons"] lessons.each do |lesson| Lesson.find_or_initialize_by(external_id: lesson['external_id']) .update_attributes(period.merge(location_id: location.id)) end end end end
Is it possible to remove this nested iteration?Is it possible to optimize this function using the gem virtus or something else?