mysql - Writing a simple incrementing counter in rails -
For each card, I would like to attach a special number to it which increases by one.
I think I can do all this in the controller.
def create @card = Card.new (params [: card]) @ card.SpecNum = @ card.SpecNum ++ ... end
Or I can obviously be slow and perhaps the best bet is to add MySQL to an auto-commission table is the problem that the number is to begin with a specific number, 1020.
Any thoughts?
Personally, I will not keep that responsibility on the database or controller; Something like this in this model:
/ app / model / card
assumption_nearn_ampaign: special_number def before_validation_on_create self.special_number = CardSpecialNumber. Next! End
/ app / model / card_special_number
def self.next! Last_number_holder = CardSpecial numbers.First if last_number_holder.nil? CardSocial number.Listen! (: Counter => 1020) Back to 1020 End Last_Number_Holter.Cant = Last_N_meter_Holter.Contter + 1_Final_N_Holder.ave save! Last_number_holder.counter
Comments
Post a Comment