# What is a simple way to increment the next record (not the "id" column)

**URL:** <https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182>\
**Category:** Rails\
**Created:** [June 13, 2009, 10:37am UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182 "2009-06-13T10:37:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 13, 2009, 10:37am UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182/1 "2009-06-13T10:37:53Z")

</div>

Hello Rails community,

I have been struggling a bit to try to find a simple solution for this  
simple requirement and have not found a DRY - simple way for this.

I have a table called “Posts” (Articles) with the following  
attributes:

Posts  
- id  
- username  
- post\_number

How do I make the post\_number get incremented for the next record?

That is:  
John creates the first post — /viewposts/john/1 – id 1  
John creates another post — /viewposts/john/2 – id 2

Jane creates the first post – /viewposts/jane/1 – id 3  
Jane creates the first post – /viewposts/jane/2 – id 4

Thank you in advance for your help!

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 13, 2009, 1:40pm UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182/2 "2009-06-13T13:40:09Z")

</div>

First you’ll need an unique index in posts with the user\_id and  
post\_number to be sure that they’ll not repeat. then you can just  
create a before\_create callback to define it:

class Post \< ActiveRecord::Base

```
before_create :set_post_number

validates_uniqueness_of :post_number, :scope => :user_id

protected

def set_post_number
    write_attribute(:post_number, Post.count( :conditions => {

```

:user\_id =\> self.user\_id } ) + 1 )  
end

end

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 13, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182/3 "2009-06-13T18:27:05Z")

</div>

2009/6/13 MaurÃ­cio Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â protected
> 
> Â Â def set\_post\_number  
> Â Â Â Â write\_attribute(:post\_number, Post.count( :conditions =\> {  
> :user\_id =\> self.user\_id } ) + 1 )  
> Â Â end
> 
> end

If any existing posts were deleted then would this repeat already used  
values? Also I think it is generating numbers based on the number of  
posts for the current user which is not quite what was asked for.

Can you not just use an auto-increment column?

Colin

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 15, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182/4 "2009-06-15T17:24:57Z")

</div>

hi,  
in oracle we can use sequence.  
in mysql use an autoincrement column, note that you have to save the  
object so that it hits the database for the column to get updated.

Also check out,  
[http://forums.mysql.com/read.php?61,143867,184106#msg-184106](http://forums.mysql.com/read.php?61,143867,184106#msg-184106)

Deepak

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 13, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/what-is-a-simple-way-to-increment-the-next-record-not-the-id-column/170182/5 "2009-06-13T21:13:26Z")

</div>

This is a great idea. Thanks a lot.

On Jun 13, 4:39 am, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
