ruby 在 Rails 中使用 uuidtools 生成短的 UUID 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14196253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:38:38  来源:igfitidea点击:

Generating a short UUID string using uuidtools In Rails

rubyruby-on-rails-3uuid

提问by Siddharth

I have to generate a unique and random string which is to be stored in database. For doing this I have used the "uuidtools" gem. Then in my controller I have added the following line:

我必须生成一个唯一的随机字符串,该字符串将存储在数据库中。为此,我使用了“uuidtools” gem。然后在我的控制器中,我添加了以下行:

require "uuidtools"

and then in my controllers create method I have declared a 'temp' variable and generating a unique and random 'uuid' string like this:

然后在我的控制器 create 方法中,我声明了一个 'temp' 变量并生成一个唯一且随机的 'uuid' 字符串,如下所示:

temp=UUIDTools::UUID.random_create

which is creating a string like this one:

它正在创建一个这样的字符串:

f58b1019-77b0-4d44-a389-b402bb3e6d50

Now my problem is I have to make it short, preferably within 8-10 character. Now how do I do it?? Is it possible to pass any argument to make it a desirable length string??

现在我的问题是我必须让它简短,最好在 8-10 个字符内。现在我该怎么做??是否可以传递任何参数使其成为理想的长度字符串??

Thanks in Advance...

提前致谢...

回答by Dogbert

You don't need uuidtools for this. You can use Secure Randomfor this.

为此,您不需要 uuidtools。您可以为此使用安全随机

[1] pry(main)> require "securerandom"
=> true
[2] pry(main)> SecureRandom.hex(20)
=> "82db4d707c4c5db3ebfc349da09c991b7ca0faa1"
[3] pry(main)> SecureRandom.base64(20)
=> "CECjUqNvPBaq0o4OuPy8RvsEoCY="

Passing 4and 5to hexwill generate 8 and 10 character hex strings respectively.

传递45tohex将分别生成 8 个和 10 个字符的十六进制字符串。

[5] pry(main)> SecureRandom.hex(4)
=> "a937ec91"
[6] pry(main)> SecureRandom.hex(5)
=> "98605bb20a"

回答by Rameshwar Vyevhare

Please see in detail, How I used securerandom in one of my project recently, definitely help you!

请详细查看,我最近如何在我的一个项目中使用securerandom,绝对对您有帮助!

create usesguid.rb file in your lib/usesguid.rb and paste below code in that -

在你的 lib/usesguid.rb 中创建 usesguid.rb 文件并粘贴下面的代码 -

require 'securerandom'

module ActiveRecord
  module Usesguid #:nodoc:
    def self.append_features(base)
      super
      base.extend(ClassMethods)  
    end

    module ClassMethods
      def usesguid(options = {})
        class_eval do
          self.primary_key = options[:column] if options[:column]
          after_initialize :create_id
          def create_id
            self.id ||= SecureRandom.uuid
          end
        end
      end
    end
  end
end
ActiveRecord::Base.class_eval do
  include ActiveRecord::Usesguid
end

add following line in your config/application.rb to load file -

在 config/application.rb 中添加以下行以加载文件 -

require File.dirname(__FILE__) + '/../lib/usesguid'

Create migration script for UUID function as mentioned below to -

为 UUID 函数创建迁移脚本,如下所述 -

class CreateUuidFunction < ActiveRecord::Migration
  def self.up
    execute "create or replace function uuid() returns uuid as 'uuid-ossp', 'uuid_generate_v1' volatile strict language C;"
  end

  def self.down
    execute "drop function uuid();"
  end
end

Here is example for contact migration, how we can use it -

这是联系人迁移的示例,我们如何使用它 -

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts, id: false do |t|
      t.column :id, :uuid, null:false 
      t.string :name
      t.string :mobile_no

      t.timestamps
    end
  end
end

Final how to use into your model

最终如何使用到您的模型中

class Contact < ActiveRecord::Base
  usesguid

end

This will help you to configure UUID for your rails application.

这将帮助您为 Rails 应用程序配置 UUID。

This can be useful for Rails 3.0, 3.1, 3.2 and 4.0 as well.

这对 Rails 3.0、3.1、3.2 和 4.0 也很有用。

Please let me know If you have any issue while using it, so simple!

如果您在使用时遇到任何问题,请告诉我,就这么简单!