# Implementing a News Feed / Activity Feed on Several Models - Recommendations?

**URL:** <https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684>\
**Category:** Rails\
**Created:** [September 28, 2010, 5:19am UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684 "2010-09-28T05:19:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 28, 2010, 5:19am UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684/1 "2010-09-28T05:19:09Z")

</div>

Hello, I’m interested in learning how to implement a News Feed /  
Activity Feed on a web app for multiple models like Books, Authors,  
Comments, etc…

Any recommendations from the group? Gems/Plugins, or experience  
personally or from others on the best/smartest way to proceed?

Thanks!

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 28, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684/2 "2010-09-28T16:08:59Z")

</div>

> **[\#87 Generating RSS Feeds - RailsCasts](http://railscasts.com/episodes/87-generating-rss-feeds)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 28, 2010, 11:15pm UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684/3 "2010-09-28T23:15:52Z")

</div>

I did it by creating a new model called FeedItem and then creating an  
observer to watch a certain subset of models and create feed items  
when they were created, updated, and deleted. FeedItems have a  
polymorphic association to the model being talked about and a user  
association for who is doing the action, and then a string field  
denoting the action taken on them (“created” or “destroyed” for  
example).

Then on each model I created a #best\_identifier method which allows  
the model to specify how it should be identified to the user in the  
feed and elsewhere in the app. So for the feed itself, I just render  
the last 30 feed items with “#{feed\_item.user\_id}  
#{feed\_item.action\_name} #{feed\_item.modified\_model.best\_identifier}  
on #{feed\_item.created\_at.to\_s(:short)}”.

Hopefully this helps! I can pastie my exact code if you’d like. There  
are some other small tidbits to add like checking for destroyed models  
(because you can’t call best identifer on them anymore), linking to  
the model and perhaps user, adding images where appropriate, …

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 29, 2010, 1:53am UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684/4 "2010-09-29T01:53:21Z")

</div>

Nice solution!

Another possibility would be to use the before\_create and delete  
methods.

Or just have the new controller pull the latest info from your other  
models by looking at the updated\_at field. This way you wouldn’t have  
to have an extra step when deleting items.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 29, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/implementing-a-news-feed-activity-feed-on-several-models-recommendations/195684/5 "2010-09-29T23:13:33Z")

</div>

@hornairs that’s very interesting. I love the idea of an observer to  
watch model CRUD actions, I have not heard of such a thing before.  
Could you show that with pastie, that seems like an elegant way to  
implement versus after\_create… across multiple models.

The FeedItems model sounds like it requires a lot of JOINs which is  
something I want to avoid. I’d like to have everything the feed  
rendering partial needs in the feedItem record for performance  
reasons. I’ve seen on StackOverflow an example of doing something like  
id, model\_name, model\_type, data {commentID: asdadasd, content:  
“asdasdad”}

What do you think of that? I’m not sure yet how to user a hash that’s  
stored like that in a column, if you know of any pointers on where I  
could learn such a thing that’d be great.

Thank you
