# Best way to handle user's topic views in a forum

**URL:** <https://www.ruby-forum.com/t/best-way-to-handle-users-topic-views-in-a-forum/153407>\
**Category:** Rails\
**Created:** [November 16, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/best-way-to-handle-users-topic-views-in-a-forum/153407 "2008-11-16T17:45:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thibaut.Me](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@Thibaut.Me](https://www.ruby-forum.com/u/Thibaut.Me)\
**Post date:** [November 16, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/best-way-to-handle-users-topic-views-in-a-forum/153407/1 "2008-11-16T17:45:49Z")

</div>

Hello,

I’m french… hope you will understand my English ^^

So I’m building a forum for my website from scratch. None of the  
already built forum like Beast or RForum suits my needs, and it’ll  
take as much time to integrate them as to create a new one.

I would like your opinion on how to handle the user’s topic views.  
The best behavior would be that every unread topic stays in this state  
as long as the user doesn’t read it, and everytime a new post is  
created the topic is marked unread for that specific user. But I think  
it’ll take to much resources to do that.  
So I’m thinking to do it the “punbb” way (not really sure but I  
remember it that way) : when a user goes back to the forum, every  
topic updated since its last visit is marked unread, until the user  
reads it. The only thing I don’t like with this approach is that I  
need to update an “last\_visit\_at” attribute on my users every time  
they request a page on the forum.  
So how would you do it ?

My structure looks like this :

1. class Forum \< ActiveRecord::Base
2. has\_many :forum\_topics
3. has\_many :forum\_posts
4. end
5. 
6. class ForumTopic \< ActiveRecord::Base
7. belongs\_to :user
8. belongs\_to :forum
9. has\_many :forum\_posts
10. end
11. 
12. class ForumPost \< ActiveRecord::Base
13. belongs\_to :user
14. belongs\_to :forum
15. belongs\_to :forum\_topic
16. end

Thanks

Thibaut

---

<div class="post-metadata">

**Author:** ![Thibaut.Me](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@Thibaut.Me](https://www.ruby-forum.com/u/Thibaut.Me)\
**Post date:** [January 26, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/best-way-to-handle-users-topic-views-in-a-forum/153407/2 "2009-01-26T21:28:37Z")

</div>

Hi,  
take a look at Forulio - Ruby on Rails based forum.  
How we did this there.

There is model:

class ReadTopic \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :topic  
belongs\_to :forum  
end

this model store last read post in topic. So when you open topic but did  
not look at last post it still stays unread.

You can look live how it works at [http://forulio.com](http://forulio.com)

and browse code:

[http://svn2.assembla.com/svn/forumbla/trunk/](http://svn2.assembla.com/svn/forumbla/trunk/)
