Reputation: 41
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?
Upvotes: 0
Views: 330
Reputation: 931
This logic, can help you
before_create :inc_acc_num //callback
def inc_acc_num
self.account_number = Account.last.try(:account_number) + 50
end
Upvotes: 1
Reputation: 11876
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
Upvotes: 2