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

  • SEARCH
  • Home
  • 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 8042881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:44:00+00:00 2026-06-05T04:44:00+00:00

In a Rails app I want to combine several models into a single activity

  • 0

In a Rails app I want to combine several models into a single activity feed.

For example, I have Post, Comment, Photo, Status models. Each one belongs to a User, an Institution, and a City. On a User/Institution/City show page I want to display a chronological list of the latest activity.

From reading around and from other questions on SO, there appear to be two schools of thought on this.

  1. Create a polymorphic ActivityFeed model where e.g. a User has_many :posts as: feedable. Then render the ActivityFeed collection on the respective show pages.
  2. Create and render a combined array e.g. @user_feed = @user_photos + @user_comment +..

But I haven’t seen much discussing the pros and cons of each approach.

Has any one else attempted something similar? What issues should I be thinking about or planning for in choosing an approach? How well do each perform (especially as the database gets larger)?

My requirements are:

  1. To display all items that belong to the parent model.
  2. To have several different types of parent model (User, Institution, City, etc).
  3. To render a different view partial for each item in the activity feed, depending on its class.

Grateful for any thoughts, personal experiences, or pointers to further information on this. Should I be looking for a third option? I’m particularly interested in the performance of creating a combined array from several queries.

Thanks

  • 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-06-05T04:44:02+00:00Added an answer on June 5, 2026 at 4:44 am

    Let me add a few more issues to your requirements

    1. ease of display all items belonging to parent model
    2. ease of allowing for different types of parent model
    3. ease of class-dependent template rendering
    4. ease of refactoring shared activity functionality
    5. ease of integrating potential activity control functionality (mark as read, subscribe, follow, share etc)
    6. ease of calculating polymorphic feed, say in reverse chronological order of activities of various types.
    7. ease of calculating aggregated activity count on a parent

    For me 5 and 6 and 7 defo tips it in favour of the first option, my code would be:

    class Activity
      belongs_to :activity, polymorphic: true
      belongs_to :user, counter_cache: true
      belongs_to :institution, counter_cache: true
      belongs_to :city, counter_cache: true
    end
    

    your scope for city newsfeed (demonstrating 1,2,6)

    city.activities.order('updated_at DESC').limit(20)
    

    note that this is a cheap search on one table only that can already give you enough for a quick list since association loading (would be .includes(:activities)) is not necessary to display something like.

    Joe from NYU, New York has posted a comment
    Mel from UCL, London has added a new photo
    

    4 is trivial if activity is a separate class and 5 also smooth with standard activity controller. for shared rendering partials like the quicklist views/activities will be a natural place.
    7 is easily done with the counter_cache.
    as a bonus, by allowing has_many :activities on each type, you could distinguish types of activities, like create, update, read, etc.

    hope this helps

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

Sidebar

Related Questions

I have a single Rails 2.2.2 app that I want to 'share' with multiple
I have a rails app and I want to deploy it on a server
I want to use mysql with a rails app (I have never used mysql
I want to integrate chargify to my rails app. I have user object and
I have a Rails app (running 3.2.2) and I want to cache my users'
I have a rails app that I'm going to host on engineyard and want
In my rails app, I want to have a sortable list as part of
I have a rails app I want to run on heroku to test. I
I have a simple class I want to include in my Rails app, but
I have this new Rails app. I installed Devise. I don't want people registering

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.