class Person < ApplicationRecord belongs_to :birth_country, class_name: Country.name has_many :admissions def stays stays = [] admissions.ordered.includes(:country).all.each_cons(2) do |a, b| stays.push( country: a.country, arrival: a.arrived_on, departure: b.arrived_on, duration: (b.arrived_on - a.arrived_on).to_i + 1 ) end stays end end