Auto numbering of childs for a parent

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

http://www.code71.com

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?

You might look into acts_as_list at GitHub - rails/acts_as_list: NOTICE: official repository moved to https://github.com/swanandp/acts_as_list.

-philip