# How to make read-times counter

**URL:** <https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624>\
**Category:** Rails\
**Created:** [May 8, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624 "2007-05-08T16:30:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ackiae86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@ackiae86](https://www.ruby-forum.com/u/ackiae86)\
**Post date:** [May 8, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624/1 "2007-05-08T16:30:52Z")

</div>

Hello all:

I wanted to realize a article counter function in my blog system.

Please help me out  
Thanks!

---

<div class="post-metadata">

**Author:** ![ackiae86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@ackiae86](https://www.ruby-forum.com/u/ackiae86)\
**Post date:** [May 8, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624/2 "2007-05-08T17:58:39Z")

</div>

Devine A. wrote:

> Hello all:
> 
> I wanted to realize a article counter function in my blog system.
> 
> Please help me out  
> Thanks!

Add a field to your blog posts table called read. Make it an int.

Every time your controller show action is called, update this count.

def show  
@post = Post.find(params[:id])  
@post.update(:read\_count = @post.read\_count +1)  
end

Or something similar.

Simple, but not very efficient.

---

<div class="post-metadata">

**Author:** ![ackiae86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@ackiae86](https://www.ruby-forum.com/u/ackiae86)\
**Post date:** [May 8, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624/3 "2007-05-08T18:35:00Z")

</div>

uhm …

add a “times\_read” integer column to your articles table

in your controller, you add something like this:

#controller  
def show  
@post = Post.find params[:id]  
@post.increment!(“times\_read”)

… rest of your code …

end

#view

This Post has been read \<%= @post.times\_read %\> times

On 8 Mai, 16:30, Devine A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ackiae86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@ackiae86](https://www.ruby-forum.com/u/ackiae86)\
**Post date:** [May 9, 2007, 10:51am UTC](https://www.ruby-forum.com/t/how-to-make-read-times-counter/103624/4 "2007-05-09T10:51:15Z")

</div>

Thorsten wrote:

> uhm …
> 
> add a “times\_read” integer column to your articles table
> 
> in your controller, you add something like this:
> 
> #controller  
> def show  
> @post = Post.find params[:id]  
> @post.increment!(“times\_read”)
> 
> … rest of your code …
> 
> end
> 
> #view
> 
> This Post has been read \<%= @post.times\_read %\> times
> 
> On 8 Mai, 16:30, Devine A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks for your help!

The increment! can increments the attribute and saves the record.

def increment!(attribute)  
increment(attribute).update\_attribute(attribute, self[attribute])  
end

One function can made it!  
Rails is very perfect!
