Add random, unique "pretty" ids to your ActiveRecord models.
gem 'pretty_id'
add_column :books, :pretty_id, :string
add_index :books, :pretty_id
class Book < ActiveRecord::Base
has_pretty_id
end
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
Array.new(options[:length]) { chars[rand(chars.length)] }.join
SecureRandom.urlsafe_base64(options[:length] / 1.333)
class Book < ActiveRecord::Base
has_pretty_id method: :urlsafe_base64, # default: :pretty
column: :another_string_column, # default: :pretty_id
length: 12, # default: 8
uniq: false # default: true
end
user = User.create
user.pretty_id # => 'a0923sdf'
user.regenerate_pretty_id
user.pretty_id # => 'lf91fs9s'
Same as above, but also calls save
on the record