Change starting id number

前端 未结 6 927
灰色年华
灰色年华 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:48

    Another possible concept might be to simply use a start_at variable in your model file?

    Such as define a base number such as start_at = 53131 and then... Make an accessor method (could call it "key") which adds your start_at number to your database's real ID before returning it.

    And you could make a attr writer method that subtracts the start_at before saving the key, that may not even be necessary depending on your implementation.

    Example in pseudo-code so bear with me.

    class FakeModel
      attr_accessible :name
      start_at = 53121
    
      def self.find_by_key(key)
        find_by_id(key-start_at))
      end
    
      def key
        (self.id+start_at)
      end
    end
    

    Not sure how practical this is or if it would even work 100% but at least you wouldn't have to modify the database to handle it.

提交回复
热议问题