# Adding a counter after the fact

**URL:** <https://www.ruby-forum.com/t/adding-a-counter-after-the-fact/135767>\
**Category:** Rails\
**Created:** [April 24, 2008, 7:53pm UTC](https://www.ruby-forum.com/t/adding-a-counter-after-the-fact/135767 "2008-04-24T19:53:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [April 24, 2008, 7:53pm UTC](https://www.ruby-forum.com/t/adding-a-counter-after-the-fact/135767/1 "2008-04-24T19:53:13Z")

</div>

## I want to add a \_count to a couple of my models but this is not a new relationship so the numbers will not start at zero. I have tried just adding an update\_attribute to my migration with the current count but for some reason the DB will not take the new number. Does the \_count field have a protection on it so people can not tamper with it? What is the proper way to add a \_count field to relationships that already exist in production i.e. is there a Rails way of doing it?

John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [April 24, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/adding-a-counter-after-the-fact/135767/2 "2008-04-24T20:23:23Z")

</div>

The point of the \_count field is to count the number of child objects  
a specific parent has, so if you were adding a count column now, I’m  
assuming you’d have to update that column with the correct numbers?

It’s just a normal field triggered by am after\_filter in rails or  
something no?

I’ve never used it, this is just my understanding of it.
