Skip to content

Instantly share code, notes, and snippets.

@JeffCohen
Created June 11, 2014 16:24
Show Gist options
  • Save JeffCohen/aa5652c05fc6676bc722 to your computer and use it in GitHub Desktop.
Save JeffCohen/aa5652c05fc6676bc722 to your computer and use it in GitHub Desktop.
Slugs

require 'digest/md5' class List < ActiveRecord::Base

has_many :entries, -> { order('position asc') }, dependent: :destroy has_many :songs, through: :entries belongs_to :user

before_create :create_slug

validates_uniqueness_of :sharing_slug

def create_slug slug = Digest::MD5.hexdigest(self.id.to_s)[0,6] # slug.split(//).shuffle.join until List.find_by(sharing_slug: slug).blank? self.update sharing_slug: slug

end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment