T
T
Tesla_Rails2014-05-18 18:58:39
Ruby on Rails
Tesla_Rails, 2014-05-18 18:58:39

The number of views of the "post"

I'm building a small news site. It has no users, like many other news resources. How can I implement a view count for each post? Maybe there is some kind of gem?
Needed to display popular posts on the page.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Arkady Butermanov, 2014-05-18
@Arkadey

1. Create an integer field :visitors in the Post model.
2. In the post controller, do something like this:

def show
...
@post.increment(:visitors)
@post.save
...
end

S
Sergey, 2014-05-18
@mastedm

On a news site, it is logical to keep the news in the cache, for example, in cache views or in general in nginx. Therefore, it is wrong to increment the counter in the post itself. I would make a call to a fake 1x1 pixel image on each page with a post, which passes the post id to the server, for example, like this:
This call will create a CounterHit entry with post_id and possibly additional information about the user. Further, this data can already be aggregated as you like: make statistics by hour, by day, "for the last 15 minutes", and so on.

A
Andrey Vershinin, 2014-05-18
@WolfdalE

I can't in ruby, but I'll suggest this: I'm assuming your news is stored in a database. So: wrap the news selection in a stored procedure, and after reading, update the number of views (if you don't have such a column, create one).
So the output of popular posts will be implemented in one request.

A
Alexander, 2014-05-19
@Aice

gem ' punching_bag '
any operations related to views, I use it myself, I advise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question