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?
*args
for? Seems irrelevant. Are these classes ActiveRecord models, or something else? What aboutLocationLessonService
— is it your own code, or a third-party remote API? It's hard to advise you how to improve the code without knowing contextual information like that. \$\endgroup\$ – 200_success Jul 1 '17 at 6:54