Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"merging" multiple models. To create a "recent activity" box

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"

like image 809
Arcath Avatar asked Jan 20 '10 08:01

Arcath


People also ask

How to merge two data sets with different models?

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

Is it possible to merge models after estimation?

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.

Can I combine multiple iddata models into one iddata object?

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.

How can pandas be used to merge data sets?

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.


1 Answers

You have to:

  1. query elements for each model
  2. merge them in a common format
  3. 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).

like image 164
Anna B Avatar answered Oct 12 '22 23:10

Anna B