Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 556529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:55:45+00:00 2026-05-13T11:55:45+00:00

How do you merge models so that I can have the last 10 Posts,

  • 0

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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:55:46+00:00Added an answer on May 13, 2026 at 11:55 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that are not related (in the db sense) but have
I have an application that uses CoreData. I previously had a class named Marker
I'm currently designing solution where the domain model & the repository can be extended
I have a question about object relational updates on join fields. I am working
I use JPA persistence for my data models with Eclipselink as the persistence provider.
it's my flight simulation application again. I am leaving the mere prototyping phase now
I'm trying to build the search for a Django site I am building, and
I am thinking about ways to create a Role Based Access Control system in
Problem description There are different categories which contain an arbitrary amount of elements .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.