The methods of the class maximumand minimumyour model will use aggregated SQL functions min()and max()to efficiently find extreme values.
span_secs = Foo.maximum(:created_at) - Foo.minimum(:created_at)
avg_secs = span_secs / (Foo.count - 1)
avg_days = avg_secs / (24 * 60 * 60)
source
share