# increment field within scope

**URL:** <https://discuss.rubyonrails.org/t/increment-field-within-scope/29736>\
**Category:** rubyonrails-talk\
**Created:** [May 23, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/increment-field-within-scope/29736 "2008-05-23T09:40:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivor\_Paul](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@Ivor\_Paul](https://discuss.rubyonrails.org/u/Ivor_Paul)\
**Post date:** [May 23, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/increment-field-within-scope/29736/1 "2008-05-23T09:40:33Z")

</div>

Hi

I have the following scenario:

A company has an account. Each account has many users. Users can make posts.

Posts will obviously have an .id field that autoincrements. However, I want a field, say post\_number, that increments within the :scope =\> :account\_id, so all the posts within a company has numbers that follow on each other.

Problem. Two users from the same account could be posting at the same time. I need a way to make sure the post\_number is unique for each account and increments from 1.

If you are using a mysql database - what would be the best way of doing this? Would you have to do the saving within a transaction?

Regards Ivor
