我有一个向用户显示的提要,其中包括 4 种不同的模型类型。
它通过将一天对象插入到提要中来模拟按天对条目进行分组。
提要按时间顺序排序和分页。
这就是我目前构建提要的方式。
def get_feed initial_feed_position=0, number_of_entries_to_get=30
# display is called on each model to get them all into a standard format
feed = (first_models + second_models + third_models + forth_models).map { |feed_entry| feed_entry.display }
feed += day_entries_for_feed(feed)
end_feed_position = initial_feed_position + number_of_entries_to_get
(feed.sort_by { |feed_entry| -feed_entry[:comparison_time].to_i })[initial_feed_position...end_feed_position]
end
def day_entries_for_feed feed
# iterate over a set of dates that contain feed entries
feed.map{ |feed_entry| feed_entry[:date] }.uniq.map do |day|
# building the day object in the standard feed entry format. fields that are not relevant to this question have been left out.
{
type: 'day',
comparison_time: (day + 24.hours - 1.second).time # to ensure that the day appears above it's corresponding entries in the feed, the comparison time is set to 1 second before the day ends
}
end
end
随着时间的推移,系统中的对象数量已经增加,现在使用这种方法构建提要需要很长时间。有更好的方法吗?
我正在使用 Rails 3.2.13、Ruby 1.9.3 和 PostgreSQL 9.1.9。