You have to:
- query elements for each model
- merge them in a common format
- sort and limit
Here is some code:
class Activity < Struct.new(:title, :text, :date); end
limit = 10
activities = []
activities += Post.all(:order => 'created_at DESC', :limit => limit).map do |post|
Activity.new(post.title, post.summary, post.created_at)
end
activities += Planet.all(:order => 'published_at DESC', :limit => limit).map do |planet|
Activity.new(planet.title, planet.message, planet.published_at)
end
activities += Message.all(:conditions => ['receiver_id = ?', current_user.id], :order => 'created_at DESC', :limit => limit).map do |message|
Activity.new(message.title, message.text, message.created_at)
end
# descending sort by 'date' field
sorted_activities = activities.sort_by(&:date).reverse
# 10 most recent elements across all models
@activities = sorted_activities[0..(limit-1)]
Of course, depending on your models, you will have to change which method is used as "title" or "text".
But if you happen to need many of such idioms, you should use Single Table Inheritance as we do in zena (a rails CMS).