How do you merge models so that I can have the last 10 Posts, Feed Entries and Private Messages displayed in order?
Posts are stored in the "Post" model and ordered on "created_at"
Feed Entries are stored in "Planet" and ordered on "published_at"
Private Messages are stored in "Message" and need to be filtered with:
:conditions => "receiver_id = #{current_user.id}"
and ordered on "created_at"
There is another way to deal with separate data sets: a model can be computed for each set, and then the models can be merged: m4 = armax (getexp (de,1), [2 2 2 1]); m5 = armax (getexp (de,2), [2 2 2 1]); m6 = merge (m4,m5); % m4 and m5 are merged into m6
It is also possible to merge models after estimation. This technique can be used to "average out" independently estimated models. If the noise characteristics on multiple datasets are different, merging models after estimation works better than merging the datasets themselves before estimation.
Multiple experiments can be packaged into a single IDDATA object, which is then usable for all estimation and analysis requirements. This technique works for both time and frequency domain iddata. It is also possible to merge models after estimation. This technique can be used to "average out" independently estimated models.
In particular, using pandas requires the data to be extracted from the data warehouse and moved to the machine where the computation and merging can happen and then sent back to the data warehouse for storage. In the case of massive data sets, this can be infeasible.
You have to:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With