Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gem not working with Rails 5 #51

Open
gregblass opened this issue Mar 13, 2016 · 5 comments
Open

Gem not working with Rails 5 #51

gregblass opened this issue Mar 13, 2016 · 5 comments

Comments

@gregblass
Copy link

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
@gregblass
Copy link
Author

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 gregblass reopened this Mar 13, 2016
@varun-raj
Copy link

@gregblass will that check for duplicates also?

@gregblass
Copy link
Author

gregblass commented Sep 21, 2016

@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.

@wbotelhos
Copy link

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.

@pywebdesign
Copy link

I just propose a pull request with rails 5 compatibility and test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants