titibouboul
titibouboul

Reputation: 1358

Views count for posts

I'm working on a social network coded with Ruby on Rails 3.2. I've created a table PostView in order to count post views by user. It has with the following columns : post_id, user_id, created_at. The idea is to store a new entry in that table every time a user sees a post (ideally adding the condition that he has not already seen it in the last 6 hours).

My problem is that I would like to do it when user loads its news feed. As many posts are loaded at the same time, I would like to do one request that will create an entry for each post viewed (I definitely don't want to do a request in a loop)

Any thoughts ?

Upvotes: 0

Views: 171

Answers (1)

Sabyasachi Ghosh
Sabyasachi Ghosh

Reputation: 2785

If i understant your question correctly then what you need is put your code in controller like

def show
 @post = Post.all
 #next you need to do a view count so create a new method
 @post_view =  PostView.create_new_post_view(@post, current_user)
end

in the Post view model

def self.create_new_post_view(post, user)
 post.each do |p|
 # put your 6 hour logic here as you have to loop through it because your logic must be valid against each post. so against each post you have to validate your logic.
 end
end

Upvotes: 2

Related Questions