Q:

Creating an unique database column with auto-increment from a starting number in Rails 4.2.5

i have a database table with this column t.integer :account_number, :limit => 9. I need this column to automatic generate on the account creation, to auto-increment by 50 for each account created and it has to start( from the first account) at value of 450230000. So i will have the account with ID = 1 and ACCOUNT_NUMBER = 450230000, account with ID = 2 and ACCOUNT_NUMBER = 450230050.
I'm using Rails 4.2.5 and SQLite as database. There is a way to have this type of auto-increment column in Rails?

A:

Add callback in model before saving your object 
before_create :set_account_number
def set_account_number
  last_account_number = Account.maximum(:account_number)
  self.account_number = last_account_number.to_i + 50
end

Do something like that in your model

