Guest User

Untitled

a guest
Feb 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.45 KB | None | 0 0
  1. class ShortLink < ActiveRecord::Base
  2.     after_initialize :default_values
  3.     before_save(:on => :create) do generate_short_url end
  4.     validates :long_url, :alternative_url, :presence => true
  5.    
  6.     private
  7.  
  8.     def generate_short_url
  9.         self.short_url = (rand(8999999999)+1000000000).alphadecimal
  10.         ShortLink.exists?(:short_url => self.short_url) ? generate_short_url : self.short_url
  11.     end
  12.    
  13.    def default_values
  14.      self.is_active ||= true
  15.    end
  16. end
Add Comment
Please, Sign In to add comment