# Auto numbering of childs for a parent

**URL:** <https://www.ruby-forum.com/t/auto-numbering-of-childs-for-a-parent/181853>\
**Category:** Rails\
**Created:** [January 19, 2010, 8:35am UTC](https://www.ruby-forum.com/t/auto-numbering-of-childs-for-a-parent/181853 "2010-01-19T08:35:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samiron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@samiron](https://www.ruby-forum.com/u/samiron)\
**Post date:** [January 19, 2010, 8:35am UTC](https://www.ruby-forum.com/t/auto-numbering-of-childs-for-a-parent/181853/1 "2010-01-19T08:35:08Z")

</div>

I have a Author model and Book model

# field\_names =\> [id, name]

class Author \< AR::Base  
has\_many :books  
end

# field\_names =\> [id, name]

class Book \< AR::Base  
belong\_to :author,  
attr\_accessor :serial\_number  
end

Now for example I have inserted two books BOOK1(id:101) and  
BOOK2(id:102) for a author AUTHOR1(id:30)

Now when I load the author with books like Author.find\_by\_id  
(30, :include =\> :books)

I want a way to set the serial\_number incrementally for each Book  
according to the order of their ID field (or may be any other logic).  
In above example BOOK1(serial\_number:1) and BOOK2(serial\_number:2)

Some more requirements:  
I need it to work even when i find Book.find\_by\_id(102) and expecting  
the serial\_number to set 2

Any better recommendation?

Samiron paul

> **[Small Talk](https://samironpaul.blogspot.com/)**

[http://www.code71.com](http://www.code71.com)

---

<div class="post-metadata">

**Author:** ![samiron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@samiron](https://www.ruby-forum.com/u/samiron)\
**Post date:** [January 19, 2010, 6:46pm UTC](https://www.ruby-forum.com/t/auto-numbering-of-childs-for-a-parent/181853/2 "2010-01-19T18:46:16Z")

</div>

> ```
> attr_accessor :serial_number
> 
> ```
> 
> In above example BOOK1(serial\_number:1) and BOOK2(serial\_number:2)
> 
> Some more requirements:  
> I need it to work even when i find Book.find\_by\_id(102) and expecting  
> the serial\_number to set 2
> 
> Any better recommendation?

You might look into acts\_as\_list at [Ruby on Rails · GitHub](http://github.com/rails/)  
acts\_as\_list.

-philip
