# Incremental counter in DB

**URL:** <https://www.ruby-forum.com/t/incremental-counter-in-db/130102>\
**Category:** Rails\
**Created:** [February 25, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102 "2008-02-25T12:45:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 25, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102/1 "2008-02-25T12:45:22Z")

</div>

I have an application that sends messages and each message that gets  
sent needs to have a unique sequence number, this sequence number is  
incremented with each message. As I understand it, I should be storing  
this sequence number in the DB rather than in a global variable.

My question is, how do I use rails and active record to define this  
table? The table doesn’t need to have any of the active record things  
like an auto incrementing integer ID, it justs needs to be a single  
column, single row table storing an integer value that I can easily  
increment.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 25, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102/2 "2008-02-25T12:56:11Z")

</div>

On 25 Feb 2008, at 12:45, Tim C. wrote:

> increment.  
> Why not just do the following in your Message model:

before\_save :assign\_sequence\_number

protected  
def assign\_sequence\_number  
last\_number = self.class.maximum(‘sequence\_number’, :conditions  
=\> [])  
if last\_number.nil?  
self.sequence\_number = 1  
else  
self.sequence\_number = last\_number+1  
end  
end

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 25, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102/3 "2008-02-25T13:01:42Z")

</div>

I did think of finding the maximum of all the sequence numbers in the  
table and then incrementing it, I just thought that it’d be quicker (as  
the message table grows large) to grab the number from a separate table  
which only stores the last number used.  
I’ll implement it using your method for now, I can always revisit it if  
there is a performance concern.  
thanks for the help

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 25, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102/4 "2008-02-25T13:43:25Z")

</div>

single row table makes no sense  
why not create Message model, backed with messages table, having all  
those activerecord things, like id (which is in fact unique sequence  
number), and other message attributes?

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 25, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/incremental-counter-in-db/130102/5 "2008-02-25T13:15:05Z")

</div>

On 25 Feb 2008, at 13:01, Tim C. wrote:

> I did think of finding the maximum of all the sequence numbers in the  
> table and then incrementing it, I just thought that it’d be quicker  
> (as  
> the message table grows large) to grab the number from a separate  
> table  
> which only stores the last number used.  
> I’ll implement it using your method for now, I can always revisit  
> it if  
> there is a performance concern.

You can also implement a Preference model. I tend to favour a  
multirecord preference table, where each record signifies one  
particular preference, in your case this preference will be  
current\_counter\_value which is an integer. I tend to mask this  
Preference model behind a few convenience methods, similar as to what  
the restful\_authentication plugin does with the current\_user method,  
i.e. providing you with a method like “counter\_number”, which  
automatically returns the current counter and increases the  
preference counter by one.

You could check out the settings plugin at [http://beautifulpixel.com/](http://beautifulpixel.com/)  
2006/02/25/settings-plugin, although i haven’t used it myself.

Best regards

Peter De Berdt
