class VirementRemise < ActiveRecord::Base has_many :virements def init self.amount = self.virements.sum(:amount_ttc) self.token = loop do token = SecureRandom.hex(6) break token unless VirementRemise.exists?(token: token) end self.save end end