Auto-increment column scoped to another column

Hi,

I have one field that needs to be incremented automatically, but be scoped to another column. So it should increment based on the last value of the previous row with the same value of the scoped column.

I'm not trying to replace ID with another primary key or anything crazy like that, just trying to auto-increment a database column!

Any idea of how to do this? Rails 3. Thanks!

Joao Silva wrote:

Hi,

I have one field that needs to be incremented automatically, but be scoped to another column. So it should increment based on the last value of the previous row with the same value of the scoped column.

Remember to figure out how it will behave with deleted records.

I'm not trying to replace ID with another primary key or anything crazy like that, just trying to auto-increment a database column!

Any idea of how to do this? Rails 3. Thanks!

acts_as_list might actually be useful here -- it does more or less this same thing with the position column.

Some databases can do this on the DB side.

Best,

I try to use the methods before or after. I think it is better to be done in the DB.