obfuscate_id
obfuscate_id copied to clipboard
Gem not working with Rails 5
When upgrading to Rails 5, bundler works for all my other gems - but when I add obfuscate_id, this is the error I get:
bundle update
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.............................................................................................................................................................................................................................................................................................................................................................................................................
Bundler could not find compatible versions for gem "rack":
In Gemfile:
rails (= 5.0.0.beta3) ruby depends on
actioncable (= 5.0.0.beta3) ruby depends on
actionpack (= 5.0.0.beta3) ruby depends on
rack (~> 2.x) ruby
rails (= 5.0.0.beta3) ruby depends on
actioncable (= 5.0.0.beta3) ruby depends on
actionpack (= 5.0.0.beta3) ruby depends on
rack-test (~> 0.6.3) ruby depends on
rack (>= 1.0) ruby
sass-rails (>= 0) ruby depends on
sprockets (< 4.0, >= 2.8) ruby depends on
rack (< 3, > 1) ruby
capybara (>= 0) ruby depends on
rack (>= 1.0.0) ruby
passenger (>= 0) ruby depends on
rack (>= 0) ruby
thin (>= 0) ruby depends on
rack (~> 1.0) ruby
Bundler could not find compatible versions for gem "rails":
In Gemfile:
obfuscate_id (>= 0) ruby depends on
rails (~> 3.2.1) ruby
rails (= 5.0.0.beta3) ruby
If people are looking for a Rails 5 compatible solution in the mean time, I simply did:
class Post < ActiveRecord::Base
before_create :generate_random_id
private
def generate_random_id
self.id = SecureRandom.uuid
end
end
Or if you are using PostgreSQL:
create_table :posts, id: :uuid do |t|
...
end
@gregblass will that check for duplicates also?
@varun-raj I would think that PostgreSQL would probably handle that automatically, but I actually use MYSQL because it does the job and I'm too lazy to switch over so I'm not sure.
That ruby method would not check for duplicates, however a random UUID has 122 random bits. Assuming perfect randomness, you can expect the first collision at around 2^61 generated UUIDs (that's the square root of 2^122). If everyone on this earth were to generate a UUID per second, that's 10,000,000,000_365_24_60_60 = 315360000000000000 UUIDs per year, which is quite close to 2^58. That is, after a few years you would get the first collisions. Unless your application gets anywhere near those numbers, you can be pretty sure that you won't get a collision if your random generator is of decent quality.
You could do something like this:
def generate_random_id
existing_post = "not_nil"
while existing_post
random_id = SecureRandom.uuid
existing_post = Post.find_by(id: random_id) # => Returns nil if not found
end
self.id = random_id
end
This of course adds a database call.
For me, I'm using it to generate random ID's for payment transactions so users can't see how many transactions I've done. By the time I've got anywhere near that many transactions, I'd be the richest person on earth.
Hi @gregblass and @varun-raj , since this repository is kind deprecated, I created a gem to do that: https://github.com/wbotelhos/idy
I tested this scenario and it works. I would love your feedback.
I just propose a pull request with rails 5 compatibility and test