# How to create a simple loop counter

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133>\
**Category:** Rails\
**Created:** [March 17, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133 "2009-03-17T16:05:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/1 "2009-03-17T16:05:42Z")

</div>

Hi all,

```
 as you may have gathered I am a newbie to rails and programming. I

```

want to count the number of enquiries in the database and produce it as  
a number in the view preferably in the index page. All I want is the  
number of items in the database and everytime ones added or deleted the  
number will go up or down.

Something like this:

Enquries Today: ‘whatever the number of them are’

Thank you clever people!!!

Your help you been greatly appreciated

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/2 "2009-03-17T16:27:55Z")

</div>

On 17 Mar., 16:05, Nish P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Enquries Today: ‘whatever the number of them are’
> 
> Thank you clever people!!!

Assuming the name of your model is Item, you can do it this way:

Enquries Today: \<%= Item.count %\>

Just replace “Item” with whatever your model is called. #count is a  
method provided by ActiveRecord, that will return the number of  
records in the table that the table belongs to, “items” for example.

–  
Cheers,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/3 "2009-03-17T16:29:05Z")

</div>

On 17 Mar., 16:27, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just replace “Item” with whatever your model is called. #count is a  
> method provided by ActiveRecord, that will return the number of  
> records in the table that the table belongs to, “items” for example.

- in the model that the table belongs to, “items” for example.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/4 "2009-03-17T16:30:15Z")

</div>

On 17 Mar., 16:28, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Thank you clever people!!!
> 
> > Assuming the name of your model is Item, you can do it this way:
> 
> > Enquries Today: \<%= Item.count %\>
> 
> > Just replace “Item” with whatever your model is called. #count is a  
> > method provided by ActiveRecord, that will return the number of  
> > records in the table that the table belongs to, “items” for example.
> 
> - in the model that the table belongs to, “items” for example.

Argh, in the table that model belongs to, of course. So sorry. 😕

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/5 "2009-03-17T16:33:42Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On 17 Mar., 16:28, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > Thank you clever people!!!
> > 
> > > Assuming the name of your model is Item, you can do it this way:
> > 
> > > Enquries Today: \<%= Item.count %\>
> > 
> > > Just replace “Item” with whatever your model is called. #count is a  
> > > method provided by ActiveRecord, that will return the number of  
> > > records in the table that the table belongs to, “items” for example.
> > 
> > - in the model that the table belongs to, “items” for example.
> 
> Argh, in the table that model belongs to, of course. So sorry. 😕

Hi thankyou for that very quick reply but I getting:

undefined method `count’ for #Enquiry:0x41698fc

I am placing this in my index page like this:

Enquries Today: \<%= enquiry.count %\>

thanks again

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/6 "2009-03-17T16:45:44Z")

</div>

Thank you for your help, that works fine now!!

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [March 17, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-create-a-simple-loop-counter/163133/7 "2009-03-17T16:42:04Z")

</div>

On 17 Mar., 16:33, Nish P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > method provided by ActiveRecord, that will return the number of  
> > > > I am placing this in my index page like this:
> 
> Enquries Today: \<%= enquiry.count %\>

Oh, that’s probably because “enquiry” is a local variable. You need to  
call the #count _class method_ on your model. So you’ll have to do it  
like this:

Enquries Today: \<%= Enquiry.count %\>

Notice, I use a capital E. The #count method is not available on  
instances of Enquiry (a specific record in your database), but as a  
class method of your model. “Enquiry” in your case.

–  
Cheers,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)
