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 4170558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:21:15+00:00 2026-05-21T00:21:15+00:00

I am implementing an activity feed for my application, much like the facebook news

  • 0

I am implementing an activity feed for my application, much like the facebook news feed. Whenever a user does something (creates a post, comments on a post, creates a photo album, comments on a photo) a row is created in an Activities table, which contains user_id, category, and data. Data is a serialized hash that is different for each type of activity (post contains post title and post id, photo comment contains something else, etc). The view then loops through all of the activities and prints something depending on the category of activity. Pretty simple, and it works. The problem is that it’s SUPER slow, so I must be doing something wrong. Here’s the code:

#activity.rb snippet

  def post?
   category == "post"
  end

  def post_comment?
   category == "post_comment"
  end

  def album?
   category == "album"
  end

  def photo_comment?
   category == "photo_comment"
  end

#controller snippet

@Activity = Activity.all(:order=> 'created_at DESC', :limit=>"5")

#view snippet

<% @Activity.each do |a| %>
 <div class="activity_item">
  <div class="avatar" style="background-image: url(<%= small_pic_url(a.user)%>) "></div>
  <div class="text"> <%= a.user.username %> 
  <% if a.post? %>
   posted <%= link_to a.data["post_title"], post_path(a.data["post_id"]) %>
  <% elsif a.post_comment? %>
   commented on <%= link_to a.data["post_title"], post_path(a.data["post_id"]) %>
  <% elsif a.album? %>
   created a <%= link_to "Photo Album", album_path(a.data["album_id"])%>
  <% elsif a.photo_comment? %>
   commented on <%= link_to "#{a.data["username"]}'s photo", upload_path(a.data["photo_id"])%>
  <% end %>
  </div>
 </div>
 <% end %>

I added a user_id index on the activity table, but that didn’t seem to do much. Rendering the 5 activity items/avatars takes over 3 seconds, so there must be a better way to handle this.

  • 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-21T00:21:15+00:00Added an answer on May 21, 2026 at 12:21 am

    Do you have an index on created_at? If you don’t, that’ll be what’s slowing you down – MySQL will have to read through every row in your activities table to make sure it got the latest five (this applies to just about anything using an ORDER BY clause).

    And a couple more suggestions, but these are minor. Might shave a few milliseconds off, but nothing on the order of three seconds:

    1. Include the user when you select your activities. This’ll let you get your activities and their users in a single DB query. :include => :user should do the trick, or there’s a Rails 3-style .include(...) method, if you prefer.
    2. I’ve heard that serialization can be kinda slow. Perhaps you could use a polymorphic association, and then choose what to display based on a.whatever_type?

    Hope this helps!

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

Sidebar

Related Questions

I am implementing an activity indicator in my application. In my application on button
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I'm implementing some naive searching in my application, and searches will take place on
I would like to have the following screen in my application: button at the
in my application , i have a textbox where user can paste Sql (mainly
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module
When implementing a needle search of a haystack in an object-oriented way, you essentially
When implementing Quicksort, one of the things you have to do is to choose
I implementing a EventQueue and get notified when AWTEvents are send. I wait till

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.