Change starting id number

前端 未结 6 914
灰色年华
灰色年华 2021-02-05 10:53

I have an \'Account\' model in Rails with its corresponding \'accounts\' table in the database. If I wipe the database and start over, the \'account_id\' field will always star

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 11:32

    A pure Ruby, database-independent approach could be:

    class MyModel
      before_create do
        self.id = [1000, (self.class.maximum(:id) || 0) + 1].max if self.id.nil?
      end
    end
    

    When you're creating lots of records at once, this may not perform so well though.

提交回复
热议问题