class ShareImage < ActiveRecord::Base has_many :image_file_share_images has_many :image_files, :through => :image_file_share_images validates :slug, :presence => true, :uniqueness => true before_validation do if !self.slug begin self.slug = SecureRandom.hex(5) end while ShareImage.exists?(:slug => self.slug) end end end